LeetCode: 500 Keyboard Row (easy)
题目:
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) {
map<char, int> m;
vector<char> s1 = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
vector<char> s2 = {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
vector<char> s3 = {'z', 'x', 'c', 'v', 'b', 'n', 'm'};
for (auto c : s1)
m.insert(pair<char, int> (c, ));
for (auto c : s2)
m.insert(pair<char, int> (c, ));
for (auto c : s3)
m.insert(pair<char, int> (c, )); vector<string> result;
for (auto w : words){
bool b = ; #判断是否在一行
char first = tolower(w[]);
int i = m[first];
for (auto c : w ){
c = tolower(c);
int j = m[c];
if (j != i){
b = ;
break;
}
}
if (b)
result.push_back(w);
}
return result;
}
};
别人的:
class Solution {
public:
vector<string> findWords(vector<string>& words) { vector<string> ans;
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' }; for(string word:words){
int one = , two = , three = ;
for(char c:word){
if(row1.count(c)) one = ;
if(row2.count(c)) two = ;
if(row3.count(c)) three = ;
if(one+two+three > ) break;
} if(one + two + three == ) ans.push_back(word);
} return ans; }
};
LeetCode: 500 Keyboard Row (easy)的更多相关文章
- 46. leetcode 500. Keyboard Row
500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...
- Leetcode#500. Keyboard Row(键盘行)
题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...
- LeetCode 500. Keyboard Row (键盘行)
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- [LeetCode] 500. Keyboard Row 键盘行
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- LeetCode 500 Keyboard Row 解题报告
题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...
- Week4 - 500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...
- 【LeetCode】500. Keyboard Row 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解 字典 + set 日期 题目地址:https ...
- 【leetcode】500. Keyboard Row
问题描述: Given a List of words, return the words that can be typed using letters of alphabet on only on ...
- 500. Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
随机推荐
- PythonCookBook笔记——文件与IO
文件与IO 所有的程序都要处理输入与输出,涉及到文本.二进制文件.文件编码和对文件名.目录的操作. 读写文本数据 需要读写各种不同编码的文本数据,使用rt模式的open()函数. 该读写操作使用系统默 ...
- wordpress系列1:安装
https://wordpress.org/download/release-archive/ 官方中文网站:https://cn.wordpress.org/ readme.html文件,可查看Wo ...
- SAP-ABAP系列 第一篇SAP简介
第一篇 SAP简介 SAP全名为System Application and Products in Data Processing.SAP目前是全世界排名第一的RP软件,号称“全球最大的企业管理解决 ...
- VC ++6.0英文版常用菜单使用参考【转载整理】
(1)File菜单 New:打开“new”对话框,以便创建新的文件.工程或工作区. Close Workspace:关闭与工作区相关的所有窗口. Exit:退出VC6环境,将提示保存窗口内容等. (2 ...
- Sum It Up POJ 1564 HDU 杭电1258【DFS】
Problem Description Given a specified total t and a list of n integers, find all distinct sums using ...
- mysql-connector-java与mysql版本的对应
记录下mysql-connector-java与mysql版本的对应关系,已方便以后参考,这是最新版本对应, 时间:2017年5月23日 官网文档地址: https://dev.mysql.com/d ...
- 【BZOJ3052】[wc2013]糖果公园 带修改的树上莫队
[BZOJ3052][wc2013]糖果公园 Description Input Output Sample Input Sample Input Sample Output 84 131 27 84 ...
- wifi 协议栈的历史的总结
google 了一下找到下面的网页关于wifi 协议栈的说明 https://www.lifewire.com/wireless-standards-802-11a-802-11b-g-n-and-8 ...
- spring cloud服务注册与发现无法发现的可能原因
1.注册中心服务端默认90秒检测一次,看服务是否还存活,不存活则删除掉服务,还存活则继续注册上去 2. spring: profiles: dev cloud: config: name: clean ...
- Golang的一些学习
在 Go 中使用命名返回变量捕获 panic 在下面代码中,如果pressButton发生panic,那么不会执行到return err,导致返回的err是nil. func doStuff() er ...