LeetCode之Keyboard Row
来源:互联网 发布:淘宝网里的东西靠谱吗 编辑:程序博客网 时间:2024/06/04 00:51
题目概述:
给定一个单词列表,返回在键盘上只能用一排字母表示的单词。
思路:
对于给定的单词列表中的每一个单词,把这个单词写进一个新的单词列表里,先判断首字母在哪一排并记录下来,再针对这个单词余下的字母进行判断,若有字母和首字母不在同一排,就把这个单词从新的单词列表中删除。
示例:
Input: ["Hello", "Alaska", "Dad", "Peace"]Output: ["Alaska", "Dad"]
代码:
class Solution {public: vector<string> findWords(vector<string>& words) { set<char> str1={'q','w','e','r','t','y','u','i','o','p'}; set<char> str2={'a','s','d','f','g','h','j','k','l'}; set<char> str3={'z','x','c','v','b','n','m'}; vector<set<char>> str {str1,str2,str3};//把三排字母放在一个vector集合里 vector<string> word=words; vector<string> Word; int count=0; for(int i=0;i<word.size();i++) { if(word[i][0]>='A'&&word[i][0]<='Z') { word[i][0]+=32; }//先把字母变为小写 for(int k=0;k<3;k++) { if(str[k].count(word[i][0])!=0) count=k; }//记录首字母在哪一排 Word.push_back(words[i]);//放进新的单词列表 for(int j=1;j<word[i].size();j++) { if(word[i][j]>='A'&&word[i][j]<='Z') { word[i][j]+=32; } if(str[count].count(word[i][j])==0) { Word.pop_back(); break; } }//判断余下字母,若和首字母不在一排就删除 } return Word; }};
总结:
STL中vector:count()算法用于统计等于某个值的对象的个数 。这里用count()算法,若某个字母存在,计算结果不等于0;若计算结果等于0,则不存在这个字母。
阅读全文
0 0
- LeetCode之Keyboard Row
- LeetCode之Keyboard Row
- LeetCode之路:500. Keyboard Row
- Leetcode-500. Keyboard Row
- 【Leetcode】500. Keyboard Row
- leetcode-500-Keyboard Row
- 【LeetCode】500. Keyboard Row
- LeetCode 500. Keyboard Row
- leetcode 500 keyboard row
- LeetCode 500. Keyboard Row
- Leetcode-Algorithms Keyboard Row
- [LeetCode]Keyboard Row - Python
- LeetCode:Keyboard Row
- leetcode 500. Keyboard Row
- Leetcode #500 Keyboard Row
- LeetCode 500 Keyboard Row
- [LeetCode]500. Keyboard Row
- LeetCode | 500. Keyboard Row
- 回溯法N皇后问题(以8为例)
- 【Fracturing & Destruction】点破小球——Unity3D中达到条件才触发的物体爆裂、炸裂、碎裂效果
- bzoj3529
- Python中os.lisrdir()编码问题
- jquery html 动态添加元素绑定事件
- LeetCode之Keyboard Row
- spring的@Transactional注解详细用法
- 关于职业生涯的一点思考
- Android版本更新
- 查找方法总结---待完善
- 解决:mybatis执行SQL语句部分参数返回NULL
- js--排序算法
- Git-2017-06-11 20:17:19
- 项目的搭建环境1