本文主要是介绍leetcode 500. 键盘行 (很简单,就是要保存每一行的字母的值,写起来很繁琐),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目解析:每个单词如果都在一行,就假如ret,如果有个字符和第一个字符不在一行,就break,接着判断下一个。
class Solution {
public:
vector<string> findWords(vector<string>& words) {
long len = words.size();
if(len == 0)
return {};
unordered_map<char, int> mp;
vector<string> ret;
mp['q'] = 1;mp['w'] = 1;mp['e'] = 1;mp['r'] = 1;mp['t'] = 1;mp['y'] = 1;mp['u'] = 1;
mp['i'] = 1;mp['o'] = 1;mp['p'] = 1;
mp['a'] = 2;mp['s'] = 2;mp['d'] = 2;mp['f'] = 2;mp['g'] = 2;mp['h'] = 2;mp['j'] = 2;
mp['k'] = 2;mp['l'] = 2;
mp['z'] = 3;mp['x'] = 3;mp['c'] = 3;mp['v'] = 3;mp['b'] = 3;mp['n'] = 3;mp['m'] = 3;
int isFirst = 0;
bool isTrue = true;
for(int i = 0; i < len; i++){
isTrue = true;
isFirst = 0;
for(int j = 0; j < words[i].size(); j++){
if(!isFirst){
if(words[i][j] < 'a')
isFirst = mp[words[i][j]+32];
else isFirst = mp[words[i][j]];
}
else if(words[i][j] < 'a'){
if(mp[words[i][j] + 32] != isFirst){
isTrue = false;
break;
}
}
else if(mp[words[i][j]] != isFirst){
isTrue = false;
break;
}
}
if(isTrue)
ret.push_back(words[i]);
}
return ret;
}
};
这篇关于leetcode 500. 键盘行 (很简单,就是要保存每一行的字母的值,写起来很繁琐)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!