500. Keyboard Row
来源:互联网 发布:linux内核分析 编辑:程序博客网 时间:2024/05/23 16:23
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.
我的答案:
class Solution {public: vector<string> findWords(vector<string>& words) { set<char> st1 {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'}; set<char> st2 {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'}; set<char> st3 {'z', 'x', 'c', 'v', 'b', 'n', 'm'}; /* st1.insert('q'); st1.insert('w'); st1.insert('e'); st1.insert('r'); st1.insert('t'); st1.insert('y'); st1.insert('u'); st1.insert('i'); st1.insert('o'); st1.insert('p'); st2.insert('a'); st2.insert('s'); st2.insert('d'); st2.insert('f'); st2.insert('g'); st2.insert('h'); st2.insert('j'); st2.insert('k'); st2.insert('l'); st3.insert('z'); st3.insert('x'); st3.insert('c'); st3.insert('v'); st3.insert('b'); st3.insert('n'); st3.insert('m');*/ vector<string> rs; for(int i = 0; i < words.size(); ++i){ int status = 0; bool flag = true; for(int j = 0; j < words[i].length(); ++j){ if(st1.find(words[i][j]) != st1.end()){ if(status == 0 || status == 1){ status = 1; }else{ flag = false; break; } } if(st2.find(words[i][j]) != st2.end()){ if(status == 0 || status == 2){ status = 2; }else{ flag = false; break; } } if(st3.find(words[i][j]) != st3.end()){ if(status == 0 || status == 3){ status = 3; }else{ flag = false; break; } } } if(flag){ rs.push_back(words[i]); } } return rs; }};
leetcode上简洁一点:
class Solution {public: vector<string> findWords(vector<string>& words) { unordered_set<char> row1 {'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'}; unordered_set<char> row2 {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'}; unordered_set<char> row3 { 'z', 'x', 'c', 'v', 'b' ,'n', 'm'}; vector<unordered_set<char>> rows {row1, row2, row3}; vector<string> validWords; for(int i=0; i<words.size(); ++i){ int row=0; for(int k=0; k<3; ++k){ if(rows[k].count((char)tolower(words[i][0])) > 0) row = k; } validWords.push_back(words[i]); for(int j=1; j<words[i].size(); ++j){ if(rows[row].count((char)tolower(words[i][j])) == 0){ validWords.pop_back(); break; } } } return validWords; }};运行时间上,初始化的时候给set赋值比挨个insert要快不少(待研究)
阅读全文
0 0
- Leetcode-500. Keyboard Row
- 【Leetcode】500. Keyboard Row
- 500. Keyboard Row
- 500. Keyboard Row
- 【LeetCode】500. Keyboard Row
- 500. Keyboard Row
- LeetCode 500. Keyboard Row
- 500. Keyboard Row
- 500. Keyboard Row
- LeetCode 500. Keyboard Row
- 500. Keyboard Row
- 500. Keyboard Row (E)
- 500. Keyboard Row
- 500. Keyboard Row
- leetcode 500. Keyboard Row
- 500. Keyboard Row
- [LeetCode]500. Keyboard Row
- 500. Keyboard Row
- PLSQL注册信息
- 数据库Sqlite基本的增删改查语句
- git
- 堆排序
- JAVA并发编程随笔【二】Comparable接口(类的自然排序 natural ordering)
- 500. Keyboard Row
- 基于Android的“健康宝”体检APP(五)
- 深入理解 Android 之 View 的绘制流程
- PHP总结的一些语法
- 编辑修改小程序信息-微信小程序开发-视频教程6
- spark 启动命令
- 7 Critical Software Testing Skills
- jvm加载class文件的原理机制
- 每日AC--POJ 1458 Common Subsequence -DP