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.
题目标签:Hash Table
题目给了我们一个words array,让我们判断每一个word 中的 chars 是否都来自于键盘上的同一行。
利用HashMap 把键盘上的3行 chars 保存:char 当作 key;行数 当作 value。
接着遍历words,检查每一个word。
Java Solution:
Runtime beats 68.36%
完成日期:06/07/2017
关键词:HashMap
关键点:char 当作 key;行数 当作 value
class Solution
{
public String[] findWords(String[] words)
{
HashMap<Character, Integer> map = new HashMap<>();
List<String> resList = new ArrayList<>(); String row1 = "qwertyuiop";
String row2 = "asdfghjkl";
String row3 = "zxcvbnm"; // set up the map
for(char c: row1.toCharArray())
map.put(c, 1); for(char c: row2.toCharArray())
map.put(c, 2); for(char c: row3.toCharArray())
map.put(c, 3); // iterate each word to check all chars are from one row
for(String word: words)
{
char[] wordChars = word.toLowerCase().toCharArray();
int rowNumber = map.get(wordChars[0]);
boolean add = true; for(char c: wordChars)
{
if(rowNumber != map.get(c))
{
add = false;
break;
} } if(add)
resList.add(word);
} String[] res = new String[resList.size()]; for(int i=0; i<res.length; i++)
res[i] = resList.get(i); return res;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
LeetCode 500. Keyboard 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' ...
- 500 Keyboard Row 键盘行
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 详见:https://leetcode.com/problems/keyboard-row/description/ C++: cl ...
- Leetcode#500. Keyboard Row(键盘行)
题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...
- 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] 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 ...
- Leetcode500.Keyboard Row键盘行
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", "Dad& ...
- 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 ...
随机推荐
- 图片选择器ImageEditContainer
1. 简介 本次demo中一共封装了两个组件:ImageEditButton 和 ImageEditContainer.其中ImageEditContainer 是在 ImageEditButton, ...
- LR接口测试---Java Vuser之jdbc查询(调试前)
在eclipse下编写好的代码: import lrapi.lr; import java.sql.Connection; import java.sql.DriverManager; import ...
- handlesocket.md
[介绍](http://www.uml.org.cn/sjjm/201211093.asp ) * 查看启动参数 `service mariadb status > st.txt` ...
- C_动态库|静态库
动态库 动态链接库简称DLL,同时以.dll 为后缀,主要用于提供代码和数据 dll 并不是所有数据都能被访问到,必须要进行导出 动态链接库在内存中始终只保存了一份数据,起到了节约内存的作用 生成动态 ...
- TypeError: 'TestCase' object is not iterable
这个异常呢其实是因为我对list没有足够熟悉 我一开始很疑惑,明明已经正确返回testcase对象了呀,为啥会报TypeError: 'TestCase' object is not iterable ...
- CAD绘制一个图象标记对象(com接口VB语言)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- [转]Js获取当前日期时间及其它操作
转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...
- orb slam2
- 模板—splay
#include<iostream> #include<cstdio> #define cin(x) scanf("%d",&x) using na ...
- poj2352 Stars【树状数组】
Astronomers often examine star maps where stars are represented by points on a plane and each star h ...