Leetcode#500. Keyboard Row(键盘行)

题目描述

给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。

示例1:

输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]

注意:

  1. 你可以重复使用键盘上同一字符。
  2. 你可以假设输入的字符串将只包含字母。

思路

把键盘中的字母和其所在行数放到map中,然后比较一个字符串中是否都来自一行。

代码实现

package HashTable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 500. Keyboard Row(键盘行)
 * 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。
 */
public class Solution500 {
    public static void main(String[] args) {
        Solution500 solution500 = new Solution500();
        String[] words = {"Hello", "Alaska", "Dad", "Peace"};
        solution500.findWords(words);
    }

    /**
     * 把键盘中的字母和其所在行数放到map中
     *
     * @param words
     * @return
     */
    public String[] findWords(String[] words) {
        String[] keyboard = {"QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"};
        List<String> res = new ArrayList<>();
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < keyboard.length; i++) {
            for (char c : keyboard[i].toCharArray()) {
                map.put(c, i);
            }
        }
        int index;
        for (String word :
                words) {
            index = map.get(word.toUpperCase().toCharArray()[0]);
            for (char c :
                    word.toUpperCase().toCharArray()) {
                if (map.get(c) != index) {
                    index = -1;
                    break;
                }
            }
            if (index != -1) {
                res.add(word);
            }
        }
        return res.toArray(new String[res.size()]);
    }
}

原文地址:https://www.cnblogs.com/wupeixuan/p/9655059.html

时间: 2024-11-09 18:05:14

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

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. 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