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:

  1. You may use one character in the keyboard more than once.
  2. 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

最新文章

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

热门文章

  1. 鼠标拖拽移动Java界面组件
  2. 4星|《OKR工作法》:关注公司的真正目标,以周为单位做计划和考核
  3. ThinkPHP---辅助方法
  4. 网络编程 - socket接收大数据
  5. 洛谷——P2709 小B的询问
  6. Springboot2.0中jpa默认创建的mysql表为myisam引擎问题
  7. software collection
  8. C,LINUX,数据结构部分
  9. codechef 写题计划
  10. Springboot 添加数据源报错