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' ...
随机推荐
- nanonets
https://app.nanonets.com/ List of Models IMAGES Images: Image Categorization Beta Input: Image Out ...
- java: private, protected, public
这三个 「可访问修饰符」,是一个老生常谈的话题了.在 C++ 中也有类似的概念. 按其修饰对象的不同,分为几种用法小记一下: 用于类 只有 public 可以修饰类:private 和 protect ...
- PostgreSQL与MySQL比較
特性 MySQL PostgreSQL 实例 通过执行 MySQL 命令(mysqld)启动实例. 一个实例能够管理一个或多个数据库.一台server能够执行多个 mysqld 实例.一个实例管理器能 ...
- MySQL 存储过程 (2)
通过存储过程查询数据库返回条数操作 第一步:登录自定义用户建立存储过程需要调用测试用到的student表,具体操作如下 (1) 登录用户
- javascript中提高代码的封装性
我出的面试题中,有一条是问如何避免页面引用JS,出现函数.变量重复.冲突的. 从大的方面讲,应该引入javascript的模块化开发,符合AMD规范之类: 从小的方面说,大概就是限定变量和函数的作用域 ...
- #ZgotmplZ go web 开发 base64 图片显示
Go Web开发,用Base64作为图片URL时遇到#ZgotmplZ的问题 - 简书 https://www.jianshu.com/p/54fc25da7c4f // var imgBase64 ...
- wepy开发
工欲善其事必先利其器 ide安装.配置] https://tencent.github.io/wepy/document.html VS Code 1. 在 Code 里先安装 Vue 的语法高亮 ...
- Java类加载器( 死磕9)
[正文]Java类加载器( CLassLoader ) 死磕9: 上下文加载器原理和案例 本小节目录 9.1. 父加载器不能访问子加载器的类 9.2. 一个宠物工厂接口 9.3. 一个宠物工厂管理 ...
- request,session,application三者关系<转>
几乎所有的Web开发语言都支持Session功能,Servlet也不例外. Servlet/JSP中的Session功能是通过作用域(scope)这个概念来实现的. 对象作用域为: page 在当 ...
- 获取app-package和app-activity的值
方法一 原文链接:http://mp.weixin.qq.com/s/KTkfmibSoaGOmDazJmZ8Sw 利用appium图形界面和已有的apk文件获取package和activity. 点 ...