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:

  1. You may use one character in the keyboard more than once.
  2. 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 (键盘行)的更多相关文章

  1. [LeetCode] 500. Keyboard Row 键盘行

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  2. 500 Keyboard Row 键盘行

    给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 详见:https://leetcode.com/problems/keyboard-row/description/ C++: cl ...

  3. Leetcode#500. Keyboard Row(键盘行)

    题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...

  4. 46. leetcode 500. Keyboard Row

    500. Keyboard Row Given a List of words, return the words that can be typed using letters of alphabe ...

  5. [LeetCode] Keyboard Row 键盘行

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  6. LeetCode 500 Keyboard Row 解题报告

    题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  7. LeetCode: 500 Keyboard Row (easy)

    题目: Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  8. Leetcode500.Keyboard Row键盘行

    给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", "Dad& ...

  9. 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 ...

随机推荐

  1. 关于 VS 调用存储过程加载很慢和SQL 执行很快的那些事

    执行同样的存储过程,调用同样的参数 在VS 中调用存储过程和传参后,到数据加载需要20秒或更多, 在SQL直接调用则不到一秒,同一个存储过程为什么有这么大的区别呢? 原因:存储过程计划失效的原因 产生 ...

  2. 【C++】智能指针简述(五):解决循环引用的weak_ptr

    总结一下前文内容: 1.智能指针通过RAII方法来管理指针:构造对象时,完成资源初始化;析构对象时,对资源进行清理及汕尾. 2.auto_ptr,通过“转移所有权”来防止析构一块内存多次.(如何转移? ...

  3. IDEA中springboot项目打包成jar

      springboot的打包方式有很多种.有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的.这里主要介绍如何打成jar进行部署.不推荐用war,因为spri ...

  4. rename命令中正则表达式的使用

    rename命令用字符串替换的方式批量改变文件名. 格式如下: rename 原字符串  目标字符串  文件(列表) 原字符串:将文件名需要替换的字符串: 目标字符串:将文件名中含有的原字符替换成目标 ...

  5. 【6.24校内test】T1 江城唱晚

    [题目背景] 墙角那株海棠,是你种下的思念. 生死不能忘,高烛照容颜. 一曲江城唱晚,重忆当年坐灯前, 青衫中绣着你留下的线. ——银临<江城唱晚> [问题描述] 扶苏是个喜欢一边听古风歌 ...

  6. TWaver动画之雷达扫描效果

    UI和功能是好的产品的两个重要因素,很多产品往往只注重功能上的设计,而忽略了UI.在这个“看脸”的时代,就算产品的功能很强大,如果UI跟不上步伐,你的产品都会在客户心中大打折扣.做安全和监控的项目中经 ...

  7. C语言scanf函数详细解释(转载)

    原文地址:https://blog.csdn.net/21aspnet/article/details/174326 scanf 函数名: scanf 功 能: 执行格式化输入 用 法: int sc ...

  8. cin的返回对象

    //有时间回来补坑 //先记上几个有用的博客 https://blog.csdn.net/candj/article/details/4419585https://www.cnblogs.com/gy ...

  9. (五)python3 整型int

    int() Python 内置函数,用于将一个字符串或数字转换为整型. int(x, base=10)#以十进制转换x为整型(默认) >>>int() # 不传入参数时,得到结果0 ...

  10. 【18】AngularJS 包含

    AngularJS 包含 在 AngularJS 中,你可以在 HTML 中包含 HTML 文件. 在 HTML 中包含 HTML 文件 在 HTML 中,目前还不支持包含 HTML 文件的功能. 服 ...