题目描述

给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。

示例1:

输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]

注意:

  1. 你可以重复使用键盘上同一字符。
  2. 你可以假设输入的字符串将只包含字母。

思路

把键盘中的字母和其所在行数放到map中,然后比较一个字符串中是否都来自一行。

代码实现

package HashTable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* 500. Keyboard Row(键盘行)
* 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。
*/
public class Solution500 {
public static void main(String[] args) {
Solution500 solution500 = new Solution500();
String[] words = {"Hello", "Alaska", "Dad", "Peace"};
solution500.findWords(words);
} /**
* 把键盘中的字母和其所在行数放到map中
*
* @param words
* @return
*/
public String[] findWords(String[] words) {
String[] keyboard = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"};
List<String> res = new ArrayList<>();
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < keyboard.length; i++) {
for (char c : keyboard[i].toCharArray()) {
map.put(c, i);
}
}
int index;
for (String word :
words) {
index = map.get(word.toUpperCase().toCharArray()[0]);
for (char c :
word.toUpperCase().toCharArray()) {
if (map.get(c) != index) {
index = -1;
break;
}
}
if (index != -1) {
res.add(word);
}
}
return res.toArray(new String[res.size()]);
}
}

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. 46. leetcode 500. Keyboard Row

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

  4. LeetCode 500. Keyboard Row (键盘行)

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

  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. BAT面试题:使用数组实现一个简单的阻塞队列

    这道题是我亲身经历的一道大厂面试题,非常值得分享! 这道题可以分为两个步骤进行编码解答,第一步是基于数组实现一个队列,第二步是实现线程阻塞. 如果是基于数组实现栈的数据结构,那么我们只需要一个指针进行 ...

  2. 关于:target与定位动画的奇怪现象

    今天在制作首页导航图特效demo时,无意发现一个奇怪的交互现象,故记录 经测试,简化了触发该现象的代码,如下: <!DOCTYPE html> <html> <head& ...

  3. 手动执行脚本可以运行,crontab自动执行无效的解决方法

    在需要执行的脚本里加入环境变量即可,如下图:

  4. JAVA基础知识回顾(面试资料)

    关于数据库知识和面试:https://www.cnblogs.com/yanqb/p/9894943.html 关于数据库知识和面试:https://www.cnblogs.com/yanqb/p/1 ...

  5. 【学习总结】GirlsInAI ML-diary day-19-面向对象编程

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day19 认识面向对象 Python使用类(class)和对象(object),进行面向对象(object-oriente ...

  6. C++通用WMI接口实现获取Windows操作系统内核版本号

    作为一名Windows开发者,能熟练掌握WMI技术,在开发Windows应用程序的时候往往能够事半功倍.今天来给大家分享一个使用WMI来获取Windows操作系统内核版本号的例子. 首先我们打开WMI ...

  7. HTTP协议与TCP/IP协议

    OSI 是7层         TCP/IP 协议是 4层. OIS 包括的层 从底到上依次为 1.物理层 2.数据链路层 3.网络层 4.传输层 5.会话层 6.表示层 7.应用层 TCP/IP  ...

  8. 安装 VMware CentOS Xmanager Xshell

    1.CentOS下载CentOS是免费版,推荐在官网上直接下载,网址:https://www.centos.org/download/DVD ISO:普通光盘完整安装版镜像,可离线安装到计算机硬盘上, ...

  9. Oracle计算两天两个日期间相差的天数

    Oracle计算两天两个日期间相差的天数: select to_date('19930411','yyyymmdd')-to_date('19890507','yyyymmdd') from dual ...

  10. Lua中ipairs和pairs的区别详解

    迭代器for遍历table时,ipairs和pairs的区别: 区别一:ipairs遇到nil会停止,pairs会输出nil值然后继续下去 区别二: , b = , x = , y = , " ...