国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

【LeetCode】500. Keyboard Row

2019-11-10 22:53:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

【鏈接】:keyboard-row 【描述】: Given a List of Words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.

這里寫(xiě)圖片描述 American keyboard

Example 1: Input: [“Hello”, “Alaska”, “Dad”, “Peace”] Output: [“Alaska”, “Dad”] Note: You may use one character in the keyboard more than once. You may assume the input string will only contain letters of alphabet. Subscribe to see which companies asked this question. 簡(jiǎn)單來(lái)說(shuō)就是找字符串,該字符串的每個(gè)字母都要在美式鍵盤(pán)的同一行即可。 【思路】第一種方法三個(gè)set 集合存儲(chǔ)鍵盤(pán)布局,利用set的find功能查找是否滿足條件。第二種方法看了一下討論區(qū),強(qiáng)大的python,居然有子集功能! 【代碼】:

/***********************【LeetCode】500. Keyboard RowAuthor:herongweiTime:2017/2/8 13:10language:C++http://blog.csdn.net/u013050857***********************/#PRagma comment(linker,"/STACK:102400000,102400000")#include <bits/stdc++.h>#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 1e5+10;const int maxm = 55;const LL MOD = 999999997;int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};inline LL read(){ int c=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();} return c*f;}/*Input: ["Hello", "Alaska", "Dad", "Peace"]//qwertyuiop//asdfghjkl//zxcvbnmOutput: ["Alaska", "Dad"]*/class Solution1 {public: vector<string> findWords(vector<string>& words) { int words_size=words.size(); vector<string> ret; unordered_set <char> row1 = { 'q','Q','w','W','e','E','r','R','t','T','y','Y','u','U','i','I','o','O','p','P' }; unordered_set <char> row2 = { 'a','A','s','S','d','D','f','F','g','G','h','H','j','J','k','K','l','L'}; unordered_set <char> row3 = { 'z','Z','x','X','c','C','v','V','b','B','n','N','m','M'}; for(auto &word:words){ bool f1=f2=f3=true; for(auto &ch:word){ if(f1){ auto it=row1.find(ch); if(it==row1.end()) f1=false; } if(f2){ auto it=row2.find(ch); if(it==row2.end()) f2=false; } if(f3){ auto it=row3.find(ch); if(it==row3.end()) f3=false; } } if(f1 || f2 || f3) ret.push_back(word); } return ret; }};class Solution2(object): def findWords(self, words): row1, row2, row3 = set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm'); ret = []; for word in words: w = set(word.lower()); if w.issubset(row1) or w.issubset(row2) or w.issubset(row3): ret.append(word); return ret;
上一篇:JUC之AQS框架

下一篇:幾個(gè)面試題

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 三台县| 日喀则市| 阿鲁科尔沁旗| 巴彦淖尔市| 金湖县| 凤冈县| 永嘉县| 分宜县| 黄石市| 安仁县| 五台县| 砚山县| 彰武县| 江口县| 灵寿县| 辰溪县| 成安县| 包头市| 平顶山市| 德庆县| 宽城| 金山区| 含山县| 中山市| 全南县| 临漳县| 泸西县| 淳安县| 卢湾区| 布尔津县| 天水市| 溧水县| 海安县| 丹江口市| 深水埗区| 陵水| 乌审旗| 灵石县| 襄汾县| 沙雅县| 镇巴县|