题目描述

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

示例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. vue页面固定锁死

  2. Django Models 查询操作

    1.准备数据表: from django.db import models class City(models.Model): name=models.CharField(max_length=32) ...

  3. 微信小程序域名

    微信小程序与第三方服务器通讯的域名必要条件1.一个已备案的域名,不是localhost.也不是127.0.0.1,域名不能加端口2.加ssl证书,也就是https://~~~3.HTTPS 服务器的 ...

  4. python 三元运算符、推导式、递归、匿名函数、内置函数

    三目运算符 # 三目(元)运算符:就是 if...else...语法糖 # 前提:简化if...else...结构,且两个分支有且只有一条语句 # 注:三元运算符的结果不一定要与条件直接性关系 cmd ...

  5. ubuntu 安装 evpp

    ubuntu 安装 evpp 来源 https://www.cnblogs.com/wisdomyzw/p/9402440.html Ubuntu虚拟机安装开源库evpp说明: EVPP为奇虎360基 ...

  6. 百度编辑器前后端二开图片上传Js Thinkphp tp5 ueditor

    百度编辑器图片上传Jsueditor.all.min.js 下载链接 链接:https://pan.baidu.com/s/1VNgw9ELgRRHKeCQheFkQTw 提取码:fnfi 使用方法: ...

  7. DP的优化总结

    一.预备知识 \(tD/eD\) 问题:状态 t 维,决策 e 维.时间复杂度\(O(n^{e+t})\). 四边形不等式: 称代价函数 w 满足凸四边形不等式,当:\(w(a,c)+w(b,d)\l ...

  8. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  9. Java基础 -- 深入理解Java异常机制

    异常指不期而至的各种状况,如:文件找不到.网络连接失败.非法参数等.异常是一个事件,它发生在程序运行期间,干扰了正常的指令流程.Java通 过API中Throwable类的众多子类描述各种不同的异常. ...

  10. 转载:Centos升级gcc

    一.检查centos 里面是否安装了gcc g++ 输入命令:rpm -qa|grep gcc*有看到就出来gcc的东西就是装了没有的话就yum install gcc* -y 二.升级gcc 对于C ...