Keyboard Row

1. Title
500. Keyboard Row
2. Http address
https://leetcode.com/problems/keyboard-row/?tab=Description
3. 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 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.

4. Code

public class Solution {
    public String[] findWords(String[] words) {

        Set<Character> hashSet1 = new HashSet<Character>();
        Set<Character> hashSet2 = new HashSet<Character>();
        Set<Character> hashSet3 = new HashSet<Character>();
        Set sets[] = new HashSet[3];
        sets[0] = hashSet1;
        sets[1] = hashSet2;
        sets[2] = hashSet3;
        hashSet1.add(‘Q‘);
        hashSet1.add(‘W‘);
        hashSet1.add(‘E‘);
        hashSet1.add(‘R‘);
        hashSet1.add(‘T‘);
        hashSet1.add(‘Y‘);
        hashSet1.add(‘U‘);
        hashSet1.add(‘I‘);
        hashSet1.add(‘O‘);
        hashSet1.add(‘P‘);

        hashSet2.add(‘A‘);
        hashSet2.add(‘S‘);
        hashSet2.add(‘D‘);
        hashSet2.add(‘F‘);
        hashSet2.add(‘G‘);
        hashSet2.add(‘H‘);
        hashSet2.add(‘J‘);
        hashSet2.add(‘K‘);
        hashSet2.add(‘L‘);

        hashSet3.add(‘Z‘);
        hashSet3.add(‘X‘);
        hashSet3.add(‘C‘);
        hashSet3.add(‘V‘);
        hashSet3.add(‘B‘);
        hashSet3.add(‘N‘);
        hashSet3.add(‘M‘);

        if (words == null || words.length <= 0) {
            return new String[0];
        }

        int len = words.length;

        String[] re = new String[len];
        int index = 0;
        for (int i = 0; i < len; i++) {
            String word = words[i].toUpperCase();
            int wlen = word.length();
            int tag = 0;
            int j = 0;
            if (sets[0].contains(word.charAt(0))) {
                tag = 0;
            } else if (sets[1].contains(word.charAt(0))) {
                tag = 1;
            } else {
                tag = 2;
            }
            j++;
            while (j < wlen) {
                if (!sets[tag].contains(word.charAt(j))) {
                    break;
                }
                j++;
            }
            if (j == wlen) {
                re[index++] = words[i];
            }
        }
        String[] res = new String[index];
        for (int i = 0; i < index; i++) {
            res[i] = re[i];
        }
        return res;

    }
}
时间: 2024-12-25 10:35:04

Keyboard Row的相关文章

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

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&q

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. 判断一棵二叉树是否平衡二叉树. 判断平衡二叉树,只需计算它