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:

  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<Character, Integer> map = new HashMap<>();
 6         List<String> resList = new ArrayList<>();
 7
 8         String row1 = "qwertyuiop";
 9         String row2 = "asdfghjkl";
10         String row3 = "zxcvbnm";
11
12         // set up the map
13         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 row
24         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<res.length; i++)
47             res[i] = resList.get(i);
48
49         return res;
50     }
51 }

参考资料:N/A  

LeetCode 题目列表 - LeetCode Questions List

时间: 2024-11-07 16:51:53

LeetCode 500. Keyboard Row (键盘行)的相关文章

Leetcode#500. Keyboard Row(键盘行)

题目描述 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"] 注意: 你可以重复使用键盘上同一字符. 你可以假设输入的字符串将只包含字母. 思路 把键盘中的字母和其所在行数放到map中,然后比较一个字符串中是否都来自一行.

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: ["A

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: ["A

500. Keyboard Row (5月26日)

解答 class Solution { public: vector<string> findWords(vector<string>& words) { vector<string> result; string first{"qwertyuiopQWERTYUIOP"}; string second{"asdfghjklASDFGHJKL"}; string third{"zxcvbnmZXCVBNM&quo

Keyboard Row

1. Title500. Keyboard Row2. Http addresshttps://leetcode.com/problems/keyboard-row/?tab=Description3. The question 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 ima

LeetCode_500. Keyboard Row

500. Keyboard Row Easy 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: Input: ["Hello", "Alaska", "Dad", "Peace&qu

LeetCode 键盘行&lt;四&gt;

500. 键盘行 题目网址:https://leetcode-cn.com/problems/keyboard-row/hints/ 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例1: 输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"] 注意: 你可以重复使用键盘上同一字符.

leetcode 500. 键盘行(Keyboard Row)

目录 题目描述: 示例: 解法: 题目描述: 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", "Dad", "Peace"] 输出: ["Alaska", "Dad"] 注意: 你可以重复使用键盘上同一字符. 你可以假设输入的字符串将只包含字母. 解法: class Solution { publ

[LeetCode] 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: ["A