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' ...
随机推荐
- caffe搭建--caffe在invidia+cpu 酷睿2Q9300 + ubuntu16.04.2上面的安装和编译过程
本文原创,转载请注明出处. ------------------------------------------------分割线-------------------------------- 概要 ...
- caffe搭建----Visual Studio 2015+CUDA8.0+CUDNN5配置Caffe-windows(BLVC)
原文来源: 来源:Angle_Cal 2016-12-19 17:32 本博主修改于2017-09-12. 版权所有,转载请注明出处. BLVC版本的Caffe-windows已经支持Vis ...
- css3背景及字体渐变
1.背景渐变: .linear { width: 100%; FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,sta ...
- Rest Api(转载)
来源:http://www.cnblogs.com/springyangwc/archive/2012/01/18/2325784.html 概述 REST 从资源的角度来观察整个网络,分布在各处的资 ...
- commons io上传文件
习惯了是用框架后,上传功能MVC框架基本都提供了.如struts2,springmvc! 可是假设项目中没有使用框架.而是单纯的使用jsp或servlet作为action,这时我们就能够使用commo ...
- 搭建React Native开发环境
搭建React Native开发环境 本文档是Mac下搭建的环境,针对的目标平台不同,以及开发 iOS 和 Android 的不同,环境搭建也有差异. Github地址:https://github. ...
- 用JS写九九乘法表
本来JS部分觉得就不是很好,结果经过一个寒假,在家的日子过的太舒适,基本把学的都快忘干净了,今天老师一说九九乘法表,除了脑子里浮现出要满足的条件,其他的都不记得了,赶快整理了一下: <scrip ...
- 【BZOJ4668】冷战 并查集
[BZOJ4668]冷战 Description 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺 ...
- c++的运算符的重载
1 什么是c++运算符的重载 c++运算符的重载就是说对+.-.>.<等运算符进行重新定义,这样的话,除了基本的类型,所有的类都可以进行基本的运算了,用起来非常方便.特别是用在各种算法中. ...
- r squared
multiple r squared adjusted r squared http://web.maths.unsw.edu.au/~adelle/Garvan/Assays/GoodnessOfF ...