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 ...
随机推荐
- 内存溢出及Jvm监控工具
内存泄露与内存溢出 内存溢出 out of memory,是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory. 内存泄露 memory leak,是指程序在申请内存后,无 ...
- JPEG图像压缩出现资源不足问题的解决
1,问题的提出 公司开发了一个图像压缩上传程序.采用Delphi语言实现.大致步骤如下: 1,上传前将文件打开装载到TJpegImage, 2,创建一个TBitmap组件,设置其大小,采用Stretc ...
- R语言学习 - 热图简化
绘制热图除了使用ggplot2,还可以有其它的包或函数,比如pheatmap::pheatmap (pheatmap包中的pheatmap函数).gplots::heatmap.2等. 相比于gg ...
- 视频剪辑生成gif格式(php外挂python程序)完美!
接到朋友的需求,朋友是做php的,让我帮忙处理php生成gif的需求.他的项目类似抖音短视频那种,就是展示出来的界面是gif动图,然后点进去是完整的视频. 我想了想,我倒是没做过php生成gif的需求 ...
- 1043 输出PATest (20 分)
题目链接:1043 输出PATest (20 分) 这道题目很简单,遍历整个字符串,统计相应字符的个数,然后按照题目要求进行输出即可. #include <bits/stdc++.h> u ...
- css--小白入门篇4
一.前文回顾 盒模型box model 什么是盒子?所有的标签都是盒子.无论是div.span.a都是盒子.图片.表单元素一律看做文本. 盒模型有哪些组成:width.height.padding.b ...
- Luogu P4016 「 网络流 24 题 」负载平衡问题
吐槽题目难度,这个题建模好像比前两个都要难,但是难度评级却比第二个要低. 解题思路 依旧是考虑如何建模和建立源点汇点.每个点的货物数量到最后都一样的话肯定是等于他们的平均值.用 $num$ 数组存储原 ...
- buf.writeUInt16BE()
buf.writeUInt16BE(value, offset[, noAssert]) buf.writeUInt16LE(value, offset[, noAssert]) value {Num ...
- CCF201509-2 日期计算 java(100分)
试题编号: 201509-2 试题名称: 日期计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 注意闰年的2月有2 ...
- poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...