题目描述

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

示例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. A Diversity-Promoting Objective Function for Neural Conversation Models论文阅读

    本文来自李纪为博士的论文 A Diversity-Promoting Objective Function for Neural Conversation Models 1,概述 对于seq2seq模 ...

  2. springMVC静态资源

    静态资源放在哪 静态资源不要放在WEB-INF里面,放在和WEB-INF同级的目录里,一起放在webapp下,原生tomcat中,这里的资源可以直接访问. springmvc配置怎么写 <!-- ...

  3. Cordova plugin

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010106153/article/details/53418528Cordova plugin工程 ...

  4. iOS 友盟错误分析-2019

    友盟的错误分析越来越人性化了 前提集成了友盟统计,并打包的时候保留了.dSYM文件 先看看效果 可以看到bug显而易见的被发现了!那个文件夹,那一行代码 那么怎么才能这样呢 首先加入符号表,就是.dS ...

  5. Day11 空时编码理论之正交空时分组码和垂直分层空时编码

    空时编码的用途: 一是获得分集增益(STBC,通过不同的发射天线发送相同传输信号的不同副本,实现空间分集,提高传输质量): 二是获得复用增益(V-BLAST在同一时隙,将不同的符号通过不同的天线发射出 ...

  6. 详解WTL应用向导

    之前向 VS2019 中添加了 WTL 应用向导,今天来分析下该应用向导安装的相关文件,最终达到拷贝相关文件到 VS2019 的相应目录中即可直接使用 WTL 应用向导的目的. 在 VS2017 之前 ...

  7. Photoshop快速给美女人像换头发

    今天给大家带来的教程是应用PS来抠出人物图片的发丝和修改头发的颜色. OK开始今天的教程 1.将素材文件拖拽进PS,CTRL+J复制一层. 2.应用快速选择工具大致的将头发部分选区出来,不需要太过仔细 ...

  8. VS + QT 出现 LNK2001 无法解析的外部符号 QMetaObject 的问题

    在一个QT项目中新建一个带QObject定义的类后 (不是继承),可能会出现LNK2001 的错误,这是由于IDE没有自动为新建的类生成 moc_XXXX.cpp 文件导致的. 一种做法是手动生成mo ...

  9. 软工+C(5): 工具和结构化(重构中, part 1...)

    // 上一篇:Alpha/Beta换人 // 下一篇:最近发展区/脚手架 目录: ** 0x01 讨论:工具/轮子 ** 0x02 讨论:结构/演进 ** 0x03 讨论:行为/活动 ** 0x04 ...

  10. MarkDownPad2基本语法

    一.换行和空格   (1)换行   行尾加两个空格   (2)空格     二.标题   在#后跟个空格再写文字,一个#是一级标题,两个#是二级标题,以此类推,支持六级标题.   示例: # 一级标题 ...