首页 > 代码库 > 500. Keyboard Row
500. 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.
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]Output: ["Alaska", "Dad"]
.随便写了份代码提交 ac了,但是我知道我写错了,因为我没有把单词变成小写去判断,可见他们数据出水了
class Solution {public: vector<string> findWords(vector<string>& words) { string s1("qwertyuiop"); string s2("asdfghjkl"); string s3("zxcvbnm"); map<char, int>mp; int l1 = s1.length(); int l2 = s2.length(); int l3 = s3.length(); int n = words.size(); vector<string> v; for (int i = 0; i < l1; ++i) mp[s1[i]] = 1; for (int i = 0; i < l2; ++i) mp[s2[i]] = 2; for (int i = 0; i < l3; ++i) mp[s3[i]] = 3; for (int i = 0; i < n; ++i) { int a = 0, b = 0, c = 0, kas = 0; for(auto x : words[i]) { char y = tolower(x); if (mp[y] == 1) { if (b || c) break; a = 1; } else if (mp[y] == 2) { if (a || c) break; b = 1; } else if (mp[y] == 3) { if (a || b) break; c = 1; } kas++; } if (words[i].size() == kas) v.push_back(words[i]); } return v; }};
500. Keyboard Row
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。