LeetCode 500. Keyboard Row (键盘行)
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
Note:
- You may use one character in the keyboard more than once.
- You may assume the input string will only contain letters of alphabet.
题目标签:Hash Table
题目给了我们一个words array,让我们判断每一个word 中的 chars 是否都来自于键盘上的同一行。
利用HashMap 把键盘上的3行 chars 保存:char 当作 key;行数 当作 value。
接着遍历words,检查每一个word。
Java Solution:
Runtime beats 68.36%
完成日期:06/07/2017
关键词:HashMap
关键点:char 当作 key;行数 当作 value
class Solution
{
public String[] findWords(String[] words)
{
HashMap<Character, Integer> map = new HashMap<>();
List<String> resList = new ArrayList<>(); String row1 = "qwertyuiop";
String row2 = "asdfghjkl";
String row3 = "zxcvbnm"; // set up the map
for(char c: row1.toCharArray())
map.put(c, 1); for(char c: row2.toCharArray())
map.put(c, 2); for(char c: row3.toCharArray())
map.put(c, 3); // iterate each word to check all chars are from one row
for(String word: words)
{
char[] wordChars = word.toLowerCase().toCharArray();
int rowNumber = map.get(wordChars[0]);
boolean add = true; for(char c: wordChars)
{
if(rowNumber != map.get(c))
{
add = false;
break;
} } if(add)
resList.add(word);
} String[] res = new String[resList.size()]; for(int i=0; i<res.length; i++)
res[i] = resList.get(i); return res;
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
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 ...
- Leetcode#500. Keyboard Row(键盘行)
题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", &quo ...
- 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] 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 ...
随机推荐
- dutacm.club_1085_Water Problem_(矩阵快速幂)
1085: Water Problem Time Limit:3000/1000 MS (Java/Others) Memory Limit:163840/131072 KB (Java/Othe ...
- js实现字符串反转
方案1: var str = "abcdef"; console.log( str.split("").reverse().join("") ...
- 05网页<div></div>块内容
网页<div></div>块内容 <header>此处为新 header 标签的内容</header> <navigation>此处为新 n ...
- 易忘小技巧--yum
写在前面:日常维护系统中,如果不是天天接触,难免会忘记一些实用小技巧,本文记录自己使用的小技巧,有新发现时,会长期更新 安装epel和dnf源 # 安装epel yum -y install epel ...
- jquery 实现点评标签 类似淘宝大众点评的 快速准时 货品完好等
111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...
- 比较synchronized和读写锁
一.科普定义 这篇博文的两个主角“synchronized”和“读写锁” 1)synchronized 这个同步关键字相信大家都用得比较多,在上一篇“多个线程之间共享数据的方式”中也详细列举他的应用, ...
- iphone6,iphone6 plus适配,旧项目出现黑线问题
问题:可能开始适配iPhone6和iPhone6 plus的朋友很快就发现,模块器头部和底部会出线一条黑线.但是在其他模拟器完全没有问题.程序也能正常跑.如下图 很清楚的看到头部有一条黑线. 解决办法 ...
- 网络配置:IP+NETMASK+GATEWAY+DNS
1. IP IP地址(英语:Internet Protocol Address)是一种在Internet上的给主机编址的方式,也称为网际协议地址.常见的IP地址,分为IPv4与IPv6两大类. IP ...
- http怎么做自动跳转https
Nginx版本 server { listen 80; server_name localhost; rewrite ^(.*)$ https://$host$1 permanent; ...
- 【Codeforces 159B】Matchmaker
[链接] 我是链接,点我呀:) [题意] 笔和笔盖 笔有颜色和直径 笔盖也有颜色和直径 笔盖和笔如果直径相等 那么称为good 如果笔盖和笔直径相等.颜色也相等,那么称为very good 问你怎样安 ...