[leetcode] 500. Keyboard Row
来源:互联网 发布:360电脑数据恢复软件 编辑:程序博客网 时间:2024/06/01 08:37
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"]
Note:
- You may use one character in the keyboard more than once.
- You may assume the input string will only contain letters of alphabet.
这道题是判断单词能否由键盘某一行中字母组合而成,题目难度为Easy。
题目比较简单,将各行字母分别存入Hash Table中,然后逐个判断数组中字符串即可。具体代码:
class Solution {public: vector<string> findWords(vector<string>& words) { vector<string> ret; unordered_map<char, int> hash; hash['Q'] = hash['q'] = 1; hash['W'] = hash['w'] = 1; hash['E'] = hash['e'] = 1; hash['R'] = hash['r'] = 1; hash['T'] = hash['t'] = 1; hash['Y'] = hash['y'] = 1; hash['U'] = hash['u'] = 1; hash['I'] = hash['i'] = 1; hash['O'] = hash['o'] = 1; hash['P'] = hash['p'] = 1; hash['A'] = hash['a'] = 2; hash['S'] = hash['s'] = 2; hash['D'] = hash['d'] = 2; hash['F'] = hash['f'] = 2; hash['G'] = hash['g'] = 2; hash['H'] = hash['h'] = 2; hash['J'] = hash['j'] = 2; hash['K'] = hash['k'] = 2; hash['L'] = hash['l'] = 2; hash['Z'] = hash['z'] = 3; hash['X'] = hash['x'] = 3; hash['C'] = hash['c'] = 3; hash['V'] = hash['v'] = 3; hash['B'] = hash['b'] = 3; hash['N'] = hash['n'] = 3; hash['M'] = hash['m'] = 3; for(auto s:words) { int row = hash[s[0]]; bool inOneRow = true; for(auto c:s) { if(hash[c] != row) { inOneRow = false; break; } } if(inOneRow) ret.push_back(s); } return ret; }};
0 0
- 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]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] 500. Keyboard Row
- LeetCode 500. Keyboard Row
- 元数据杀人?我们已被网络空间绑架
- 权志龙|instagram粉丝超过1400百万|欧巴特辑
- 外网链接内网:金万维端口映射、路由器设置
- android webview 加载本地文件
- Fragment全解析系列:那些年踩过的坑以及解决方案
- [leetcode] 500. Keyboard Row
- C++编译时遇到“错误:‘SomeClass’没有命名一个类型”
- Windows10 VirtualBox 客户机自启动
- 用手机访问电脑本地Apache下的静态文件方法(坑)
- Eclipse或MyEclipse更换jdk版本
- Redis命令总结
- 第二天过期时间设置问题
- 将Excel中的数据导入到 oracle中
- Python 删除字符