题目来源:链接: [https://leetcode-cn.com/problems/keyboard-row/].
500.键盘行
1.问题描述
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
示例:
输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]
注意:
1. 你可以重复使用键盘上同一字符。
2. 你可以假设输入的字符串将只包含字母。
2.我的解决方案
一开始我的思路是对的,但是代码的执行力度不够。
主要还是基本功不扎实,要学会灵活运用 STL 标准库函数。
第二次的代码用了string 类的 find 函数。
第一次的代码好low啊,哈哈哈哈
第一次蠢蠢的代码如下:
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> res;
int n1 = 0, n2 = 0, n3 = 0;
for(int i = 0; i < word.size(); i++)
{
for(int j = 0; j < word[i].size(); j++)
{
if(word[i][j] == 'x' || word[i][j] == 'z' || word[i][j] == 'c' || word[i][j] == 'v' || word[i][j] == 'b' || word[i][j] == 'n' || word[i][j] == 'm' || word[i][j] == 'X' || word[i][j] == 'Z' || word[i][j] == 'C' || word[i][j] == 'V' || word[i][j] == 'B' || word[i][j] == 'N' || word[i][j] == 'M')
{
n1++;
}
else if(word[i][j] == 'a' || word[i][j] == 's' || word[i][j] == 'd' || word[i][j] == 'f' || word[i][j] == 'g' || word[i][j] == 'h' || word[i][j] == 'j' || word[i][j] == 'k' || word[i][j] == 'l' || word[i][j] == 'A' || word[i][j] == 'S' || word[i][j] == 'D' || word[i][j] == 'F' || word[i][j] == 'G'|| word[i][j] == 'H'|| word[i][j] == 'J'|| word[i][j] == 'K'|| word[i][j] == 'L')
{
n2++;
}
else
{
n3++;
}
}
}
}
};
最终代码如下:
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> res;
string s1 = "ZXCVBNMzxcvbnm";
string s2 = "ASDFGHJKLasdfghjkl";
string s3 = "QWERTYUIOPqwertyuiop";
for(int i = 0; i < words.size(); i++)
{
int n1 = 0, n2 = 0, n3 = 0;
for(int j = 0; j < words[i].size(); j++)
{
if(s1.find(words[i][j]) != -1)
{
n1++;
}
if(s2.find(words[i][j]) != -1)
{
n2++;
}
if(s3.find(words[i][j]) != -1)
{
n3++;
}
}
if(n1 == words[i].size() || n2 == words[i].size() || n3 == words[i].size())
{
res.push_back(words[i]);
}
}
return res;
}
};
3.大神们的解决方案
思路差不多,只是多了个大小写转换
大神1:
class Solution {
public:
vector<string> findWords(vector<string>& words) {
string L1 = "QWERTYUIOP";//"QWERTYUIOPqwertyuiop";
string L2 = "ASDFGHJKL";//"ASDFGHJKLasdfghjkl";
string L3 = "ZXCVBNM";//"ZXCVBNMzxcvbnm";
vector<string> Fwords;
for (int i = 0; i < words.size(); i++)
{
string strTemp(words[i]);
int sum1 = 0, sum2 = 0, sum3 = 0;
transform(words[i].begin(), words[i].end(), strTemp.begin(), ::toupper);
for (int j = 0; j < strTemp.size(); j++)
{
if (L1.find(strTemp[j]) != -1)sum1++;
if (L2.find(strTemp[j]) != -1)sum2++;
if (L3.find(strTemp[j]) != -1)sum3++;
}
if (sum1 == strTemp.size()) Fwords.push_back(words[i]);
if (sum2 == strTemp.size()) Fwords.push_back(words[i]);
if (sum3 == strTemp.size()) Fwords.push_back(words[i]);
}
return Fwords;
}
};
大神2:
感觉是用了哈希算法实现的, map 键值对。等我待研究。。。
class Solution {
public:
vector<string> findWords(vector<string>& words) {
string first1="QWERTYUIOP",second1="ASDFGHJKL",third1="ZXCVBNM";
map <char,int> Hash;
for(auto it : first1)
Hash[it]=1,Hash[it+32]=1;
for(auto it : second1)
Hash[it]=2,Hash[it+32]=2;
for(auto it : third1)
Hash[it]=3,Hash[it+32]=3;
vector <string> ans;
for(auto it : words) {
bool flag=1;
if(it.size()==1) {
ans.push_back(it);
continue;
}
int temp=Hash[it[0]];
for(int i=1;i<it.size();i++)
if(Hash[it[i]]!=temp) {
flag=0;
break;
}
if(flag)
ans.push_back(it);
}
return ans;
}
};
4.我的收获
熟练掌握 STL 标准模板库啊啊 啊
加油!!~~~
2019/3/12 胡云层 于南京 21