[leetcode-500-Keyboard Row]
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.
思路:
首先判断第一个字符是属于键盘上的哪一排,然后判断接下来的每一个字符是否都在这一排里出现。
bool can(string str, vector<unordered_set<char>>& rows)
{
int row = ;
for (int k = ; k<; ++k)
{
if (rows[k].count((char)tolower(str[])) > ) row = k;
}
for (int i = ;i<str.size();i++)
{
if (rows[row].count((char)tolower(str[i])) == ) return false;
}
return true;
}
vector<string> findWords(vector<string>& words)
{
vector<string> ret;
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 };
for(int i = ; i < words.size();i++)
{
if (can(words[i], rows))
{
ret.push_back(words[i]);
}
}
return ret;
}
参考:
https://discuss.leetcode.com/topic/77761/c-solution
[leetcode-500-Keyboard Row]的更多相关文章
- 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 ...
- LeetCode: 500 Keyboard Row (easy)
题目: 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' ...
随机推荐
- 禁止LISTCTRL表头拖动
禁止ListCtrl表头拖动(Prevent CListCtrl column resizing) /*The header control in the ListView control sends ...
- git笔记--git@OSC
之前安装了git,用了不久就升级系统了,发现又忘记了步骤,虽然网上有很多教程,但寻找需要浪费太多的时间,由于github连接比较慢,所以使用了开源中国的托管http://git.oschina.net ...
- PHP 中级内容
1.面向对象编程(OOP) 2.模板引擎(smarty) 3.MYSQL(中级操作) 数据库抽象层(PDO): 4.Ajax(异步刷新) 5.Js框架(jQuery) Xml+JSON数据 ...
- Oracle 只导出某个用户下的表及数据
今天某大牛问我要之前我参与的一个系统的代码及库,我捣鼓下,发给了他. 他很诧异的问:这个库有这么大么 我说 因为当时是专门新建了一个实例,用户也是系统用户,所以导出的时候是导出的整个数据库 他 ZZ ...
- Python爬虫02——贴吧图片爬虫V2.0
Python小爬虫——贴吧图片爬虫V2.0 贴吧图片爬虫进阶:在上次的第一个小爬虫过后,用了几次发现每爬一个帖子,都要自己手动输入帖子链接,WTF这程序简直反人类!不行了不行了得改进改进. 思路: 贴 ...
- Centos7 最小系统安装Redis
最近想写一个调度系统,一顿查资料之后决定用Django+Celery+Redis实现,前面两个都已经有了,就缺一个Redis,所以准备安装一个Redis 获取Centos7并配置网络 获取Centos ...
- 基于cookie使用过滤器实现客户每次访问自登陆一次
原创声明:本文为本人原创作品,绝非他处摘取,转载请联系博主 相信大家在各大网站都会遇到,登录时,在登录框出现下次免登陆/一个月免登陆的类似选项,本次博文就是讲解如何实现,在这记录一下,也算是做个备忘录 ...
- 生成JSON数据--fastjson(阿里)方法
fastjson(阿里)方法生成JSON数据: 与Gson类似,创建相应类,再使用JSON.toJSONString()添加对象 要求:生成如下JSON数据 1.{"age":3, ...
- eclipse如何导入项目和文件
在左侧创建项目区,右击鼠标-->选择Import-–>General-> Existing Projects into workPlace-->选择最下方Next-->点 ...
- 浏览器本地储存方式有哪些?cookie、localStorage、sessionStorage
现阶段,浏览器提供的储存方式常用的有三种,cookie.localStorage.sessionStorage 1.cookie 概念:cookie 是浏览器中用于保存少量信息的一个对象 基本特征: ...