leetcode.500.Keyboard Row
来源:互联网 发布:evernote是什么软件 编辑:程序博客网 时间:2024/06/06 08:31
Description
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.
sln
- 创建一个数字,记录26个字符分别属于哪一行(一个由0,1,2组成的数组)
- 遍历每个单词,可以用O(1)的时间确定字母属于哪一行,那么可以用O(m)的时间确定整个单词是否可以用键盘的其中一行打出来,其中m为字符串长度
- 用O(n * m)的时间可以完成整个任务,c++实现如下
class Solution {public: vector<string> findWords(vector<string>& words) { vector<string> ret; int belongTo[] = {1, 2, 2, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 0, 2}; for (vector<string>::iterator it = words.begin(); it != words.end(); it++) { string word = *it; int group = -1; bool flag = true; for (int i = 0; i < word.length(); i++) { int cgroup = belongTo[toLower(word[i]) - 'a']; if (group != -1 && group != cgroup) { flag = false; break; } else { group = cgroup; } } if (flag) { ret.push_back(word); } } return ret; }private: char toLower(char a) { if (a >= 'A' && a <= 'Z') { return 'a' + (a - 'A'); } return a; }};
阅读全文
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
- [SIM]如何关闭SIM热插拔功能
- web端设计和web前端开发的区别
- android studio cmake 配置.a连接库
- Ndk开发
- APP扫码登录
- leetcode.500.Keyboard Row
- 事件拦截机制简单分析
- HashMap和Hashtable的区别
- Java组合模式
- 《参禅与悟道》——浅谈人生
- 位移实现乘以2的倍数的乘法
- jwPlayer API
- 在Windows下基于VS2012配置openNI
- 使用RecyclerView实现的瀑布流高度自适应