Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C++ - 500. Keyboard Row
題目:

解答:
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> ans;
unordered_map<int, int> row;
string s1 = "qwertyuiop";
string s2 = "asdfghjkl";
string s3 = "zxcvbnm";
for (char c : s1) {
row[c] = 1;
}
for (char c : s2) {
row[c] = 2;
}
for (char c : s3) {
row[c] = 3;
}
for (string w : words) {
bool need = true;
int k = row[tolower(w[0])];
for (char c : w) {
if (row[tolower(c)] != k) {
need = false;
break;
}
}
if (need) {
ans.push_back(w);
}
}
return ans;
}
};