LeetCode 500. Keyboard Row (键盘行)
2024-08-31 00:35:08
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
最新文章
- http 缓存相关学习
- [转]SpringMVC拦截器简单教程
- 应用HTK搭建语音拨号系统4: 识别器评估
- 2016/12/14---- C3P0
- 0329 复利计算器5.0 Juint单元测试 组员 254列志华 253韩麒麟
- How to move the user document folder to D disk[Windows 7]
- myeclipse 打开xml jsp页面慢 有时候会自动退出
- REST和SOAP Web Service的比较
- html5权威指南:设置文本样式
- JS跨域代码
- ASP.NET MVC5 中百度ueditor富文本编辑器的使用
- linux里所有命令都不存在
- UNIX网络编程——经常使用的套接字选项
- 关于background-size 的一点小坑
- VMware Workstation:安装windows xp系统
- Linux 命令 —— iconv 转换编码
- postgresql-10.1-3-windows-x64 安装之后,起动pgAdmin 4问题(win10)
- ECSHOP后台登陆后一段时间不操作就超时的解决方法
- 文档设计也需要坚持DRY原则--支付中心应用部署结构图完善
- DOS命令之at命令详解