LeetCode——Keyboard Row
LeetCode——Keyboard Row
Question
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.
American keyboard
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.
Answer
class Solution {
public:
vector<string> findWords(vector<string>& words) {
string str1 = "qwertyuiop";
string str2 = "asdfghjkl";
string str3 = "zxcvbnm";
vector<string> res;
for (string str : words) {
char c = str[0] >= 97 ? str[0] : str[0] + 32;
if (str1.find(c) != string::npos) {
if (judge(str, str1))
res.push_back(str);
} else if (str2.find(c) != string:: npos) {
if (judge(str, str2))
res.push_back(str);
} else {
if (judge(str, str3))
res.push_back(str);
}
}
return res;
}
int judge(string str, string str1) {
int flag = 1;
for (int i = 1; i < str.length(); i++) {
char c = str[i] >= 97 ? str[i] : str[i] + 32;
if (str1.find(c) == string::npos) {
flag = 0;
break;
}
}
return flag;
}
};
LeetCode——Keyboard Row的更多相关文章
- [LeetCode] Keyboard Row 键盘行
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- LeetCode: Keyboard Row
代码长了些,但还是比较简单的 public class Solution { public String[] findWords(String[] words) { List<String> ...
- 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 ...
- 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 (键盘行)
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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解 字典 + set 日期 题目地址:https ...
- leetcode算法: Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
随机推荐
- C# Remoting双向通信
闲来无事想玩玩双向通信,实现类似QQ的互发消息的功能.于是乎开始学习.Net Remoting. .Net Remoting 是由客户端通过Remoting,访问通道以获得服务端对象,再通过代理解析为 ...
- 解决scipy安装(pip install scipy)失败,以及其他问题
解决scipy安装(pip install scipy)失败,以及其他问题 解决: 1.在scipy官方库中并没有适合Windows的python3.6相关版本,故需要在网址http://www.lf ...
- 巨蟒python全栈开发-第20天 核能来袭-约束 异常处理 MD5 日志处理
一.今日主要内容 1.类的约束(对下面人的代码进行限制;项目经理的必备技能,要想走的长远) (1)写一个父类,父类中的某个方法要抛出一个异常 NotImplementedError(重点) (2)抽象 ...
- Java 常用语法和数据结构
Collection 首先Java中的collection都是支持泛型和类型安全 由于Java单根继承, 所以不指定, 可以在collection里面放任何对象, collection会都当作obje ...
- Babel编译
Babel的目的就是让你可以使用最新的标准来开发,然后把兼容的问题交给它来完成.比如我如何在使用ES6的语法写完之后将其转换为ES5满足通用性呢? 先用这个最常用的Babel的用法来引入吧. 一 首 ...
- unix_timestamp 和 from_unixtime 时间戳函数 区别
1.unix_timestamp 将时间转化为时间戳.(date 类型数据转换成 timestamp 形式整数) 没传时间参数则取当前时间的时间戳 mysql> select unix_time ...
- 面试题15:链表中倒数第K个结点
输入一个链表,输出该链表中倒数第k个结点. 方法1: 这个解法要循环两次链表 /* public class ListNode { int val; ListNode next = null; Lis ...
- 内置模块(time、random、hashlib、os)
简介: 模块:本质上就是一个.py文件,使用其中的函数. 模块分为:内置函数.第三方模块.自定义模块. 今天学习的就是Python的内置函数. 回到顶部 一.time模块 1.时间的表示形式 在Pyt ...
- Android图片加载框架Picasso最全使用教程4
通过前几篇的学习,我们已经对Picasso的加载图片的用法有了很深的了解,接下来我们开始分析Picasso为我们提供的其他高级功能及内存分析,Let’sGo ! Picasso进行图片的旋转(Rota ...
- pgadmin3
一般性 \copyright 显示PostgreSQL的使用和发行许可条款 \g [文件] or; 执行查询 (并把结果写入文件或 |管道) \gset [PREF ...