Leetcode#500. Keyboard Row(键盘行)
题目描述
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。

示例1:
输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]
注意:
- 你可以重复使用键盘上同一字符。
- 你可以假设输入的字符串将只包含字母。
思路
把键盘中的字母和其所在行数放到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(键盘行)的更多相关文章
- [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 ...
- 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 500. Keyboard Row (键盘行)
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- [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 ...
随机推荐
- 网络流 P2770 航空路线问题
#include <cstdio> #include <cstdlib> #include <map> #include <queue> #includ ...
- python从开始到放弃的途中一直很菜的day13
一直很菜的今天又来了两个新的概念,一个是装饰器,一个是推导式,装饰器好比女生出门前需要化妆的准备,推导式也称为生成式.先说装饰器吧,装饰器其实本质也是一个函数,并用@+函数名装饰到其他函数上,当这个其 ...
- C# .NET 0配置使用Wcf(半成品)
设计原则:万物皆对象 背景:微软提供了一套强大的通信框架Wcf,了解请看百度百科:ttps://baike.baidu.com/item/Wcf/7374854?fr=aladdin 虽然这套通信框架 ...
- C语言报错:error: expected ‘while’ at end of input } ^
在建线程池过程当中遇见上图所示错误: 解决方法: Linux中定义: SYNOPSIS #include <pthread.h> void pthread_cleanup_push(voi ...
- 03-MySQL表操作
MySQL表操作 1.介绍 表就相当于文件,表中的一条记录就相当与文件的一行内容,不同的是,表中的一条记录有对应的标题,成为表的字段. 2.创建表 2.1语法 create table 表名( 字段名 ...
- c#枚举位运算操作
抛出预设问题 需要有一个npc需要在一周中的,周一,周二,周三会出现,其他时间不可见 解决问题 因为一周时间是固定的,所以创建枚举类型比较合适,如下 enum Days { None, Sunday, ...
- AngularJS路由变化 监听方法
#使用AngularJS时,当路由发生改变时,我们需要做某些处理,此时可以监听路由事件,常用的是$routeStartChange, $routeChangeSuccess ##使用场景:在路由配置文 ...
- vue-cli3.0 flexible&px2rem 解决第三方ui组件库样式问题
背景 在项目使用lib-flexible还有postcss-px2rem实现移动端适配,当我们引入第三方的样式组件库,发现一个问题.那些组件库的样式都变小了. 问题原因 变小的主要原因是第三库 css ...
- SQL进阶随笔--case用法(一)
SQL进阶一整个是根据我看了pdf版本的整理以及自己的见解整理.后期也方便我自己查看和复习. CASE 表达式 CASE 表达式是从 SQL-92 标准开始被引入的.可能因为它是相对较新的技术,所以尽 ...
- redis的三种集群方式
redis有三种集群方式:主从复制,哨兵模式和集群. 1.主从复制 主从复制原理: 从服务器连接主服务器,发送SYNC命令: 主服务器接收到SYNC命名后,开始执行BGSAVE命令生成RDB文件并使用 ...