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' ...
随机推荐
- Delphi TreeView – 自动展开树形结构
Delphi TreeView – 自动展开树形结构 当处理完TreeView控件树形结构的数据后,需要默认自动全部展开,可以用到TreeView的Expanded属性. 1 2 3 4 5 6 7 ...
- flex组合流动布局实例---利用css的order属性改变盒子排列顺序
flex弹性盒子 <div class="container"> <div class="box yellow"></div> ...
- SharePoint BI
本篇博客主要针对SharePoint BI整体结构进行整理,为读者分析几种Sharepoint BI场景 先附一张自己做的结构图:
- MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%) MylSAM平均每秒Key Buffer读命中率(%) MylSAM平均每秒Key Buffer写命中率(%)
MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%)MylSAM平均每秒Key Buffer读命中率(%)MylSAM平均每秒Key Buff ...
- mysql几种引擎和使用场景
https://blog.csdn.net/cool_wayen/article/details/79585277 数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建. ...
- pycharm-->github / github-->pycharm
一.pycharm -->发布到--> github:1.github 账号密码 https://github.com/2.git 下载安装 https://git-scm.com/3.配 ...
- 利用Django中间件middleware解决用户未登录问题(转)
add by zhj: Django的中间件一般用于处理通用性的问题,分为五种,按处理顺序为request_middleware,view_middleware,exception_middlewar ...
- pandas 修改指定列中所有内容
如下图: 读取出来的 DataFrame “code” 列内容格式为:“浪潮信息(000977.XSHE)” 格式,目标效果是:000977.XSHE 代码: df["code"] ...
- Docker Libnetwork Bridge插件实现代码分析----创建网络部分
// drivers/bridge/bridge.go // Create a new network using bridge plugin 1.func (d *driver) CreateNet ...
- 007-Centos 7.x 安装 Mysql 5.7.13
1. 下载mysql的repo源 CentOS 7.2的yum源中默认没有mysql,要先下载mysql的repo源 wget http://repo.mysql.com/mysql57-commun ...