博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 500. Keyboard Row (键盘行)
阅读量:4654 次
发布时间:2019-06-09

本文共 2016 字,大约阅读时间需要 6 分钟。

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.

American keyboard

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

1 class Solution  2 { 3     public String[] findWords(String[] words)  4     { 5         HashMap
map = new HashMap<>(); 6 List
resList = new ArrayList<>(); 7 8 String row1 = "qwertyuiop"; 9 String row2 = "asdfghjkl";10 String row3 = "zxcvbnm";11 12 // set up the map13 for(char c: row1.toCharArray())14 map.put(c, 1);15 16 for(char c: row2.toCharArray())17 map.put(c, 2);18 19 for(char c: row3.toCharArray())20 map.put(c, 3);21 22 23 // iterate each word to check all chars are from one row24 for(String word: words)25 {26 char[] wordChars = word.toLowerCase().toCharArray();27 int rowNumber = map.get(wordChars[0]);28 boolean add = true;29 30 for(char c: wordChars)31 {32 if(rowNumber != map.get(c))33 {34 add = false;35 break;36 }37 38 }39 40 if(add)41 resList.add(word);42 }43 44 String[] res = new String[resList.size()];45 46 for(int i=0; i

参考资料:N/A  

 

LeetCode 题目列表 - 

转载于:https://www.cnblogs.com/jimmycheng/p/7824947.html

你可能感兴趣的文章
linux共享库,以及/etc/ld.so.conf文件的应用【转】
查看>>
Python 爬虫(1)基础知识和简单爬虫
查看>>
[经验] Unity3D 里怎么制作天空盒(skybox)
查看>>
ViewPager和View组合 实现页面的切换
查看>>
使用PagerSlidingTabStrip实现顶部导航栏
查看>>
调用摄像头和相册
查看>>
jQuery.事件对象
查看>>
CSS之属相相关
查看>>
整理了一下自己买过的计算机书
查看>>
解决py2exe error: MSVCP90.dll: No such file or directory
查看>>
java RSA实现私钥签名、公钥验签、私钥加密数据、公钥解密数据
查看>>
Erlang 练习题
查看>>
数据挖掘十大算法总结--核心思想,算法优缺点,应用领域
查看>>
GDALWarp设置GDALWarpOptions::dfWarpMemoryLimit过大时处理失败
查看>>
libubox组件(2)——blob/blobmsg (转载 https://segmentfault.com/a/1190000002391970)
查看>>
建立RSA协商加密的安全信道
查看>>
博客园的手机版(非官方) MVC+jQuery.Mobile
查看>>
jmeter在linux上运行
查看>>
js 逻辑与 逻辑或
查看>>
“请求/响应”模型
查看>>