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 ...
随机推荐
- 一) Spring 介绍、IOC控制反转思想与DI依赖注入
一.spring介绍1.IOC反转控制思想(Inversion of Control)与DI依赖注入(Dependency Injection)2.AOP面向切面的编程思想与动态代理3.作用:项目的粘 ...
- typeScript面对对象篇二
接口 接触过面向对象的后端语言的应该对接口很熟悉,只接触过前端的对接口会有点陌生,在维基百科中对OOP中接口的定义是这样的: 在面向对象的语言中,术语interface经常被用来定义一个不包含数据和逻 ...
- 验证码的实现py3
import randomflag = 1try_=0while (flag): try_ +=1 yan = "" for i in range(0,4): cun=random ...
- linux ps aux 各列内容说明
[root@zabbix3 ~]# ps auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot ...
- 如何在Linux中轻松删除源安装的软件包?
第1步:安装Stow 在这个例子中,我们使用的是CentOS,因此我们需要扩展的EPEL库.您可以使用以下命令安装它们:yum install epel-release然后,下面这段命令:yum in ...
- axios拦截http拦截
一,判断登录页面 const routes = [ { path: '/', name: '/', component: Index }, { path: '/repository', name: ' ...
- x86汇编寄存器,函数参数入栈说明
https://en.wikipedia.org/wiki/X86_calling_conventions
- python一(字符串,字典)
list操作 name = ['小王','小米','小张','王强','张三','李四'] name.append('黄霑')#添加元素在最后一个 name.insert(,'王五')#指定下标插入元 ...
- Bootstrap 模态框(Modal)插件id冲突
<!DOCTYPE html><html><head> <meta charset="utf-8"> <titl ...
- ACM在线模板
转载自:https://blog.csdn.net/f_zyj/article/details/51594851 Index 分类细则 说起分类准则,我也是很头疼,毕竟对于很多算法,他并不是单调的,而 ...