3.Keyboard Row

leetcode地址:https://leetcode.com/problems/keyboard-row/description/

题意:给你一个字符串数组,输出一个新的数组,这个新的数组中的每一个字符串所有的字符属于键盘上的同一行。

 1     public String[] findWords(String[] words) {
 2         String[] tmp = new String[]{"qwertyuiop","asdfghjkl","zxcvbnm"};
 3         Map<Character,Integer> map = new HashMap<Character,Integer>();
 4         for(int i=0;i<tmp.length;i++)
 5         {
 6             char[] chars = tmp[i].toCharArray();
 7             for(char a:chars)
 8                 map.put(a,i);
 9         }
10
11         List<String> result = new ArrayList<String>();
12         for(int i=0;i<words.length;i++)
13         {
14             String str = words[i].toLowerCase();
15             int index = map.get(str.charAt(0));
16             boolean flag = true;
17             for(char a:str.toCharArray())
18             {
19                 if(map.get(a) != index)
20                     flag = false;
21             }
22             if(flag)
23                 result.add(words[i]);
24         }
25         return result.toArray(new String[0]);
26
27     }
时间: 2024-10-26 20:55:44

3.Keyboard Row的相关文章

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(键盘行)

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

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

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

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

[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

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. American keyboard Example 1:Input: ["Hello", "Alaska", "Dad", "Peace"]

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

18.Keyboard Row

题目描述: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 判断一棵二叉树是否平衡二叉树. 判断平衡二叉树,只需计算它