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; } };
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号: