leetcode 500. 键盘行(Keyboard Row)

目录

  • 题目描述:
  • 示例:
  • 解法:

题目描述:

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

示例:

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

注意:

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

解法:

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        string s1 = "qwertyuiopQWERTYUIOP";
        string s2 = "asdfghjklASDFGHJKL";
        // string s3 = "zxcvbnmZXCVBNM";
        vector<int> mp(128, 0);
        for(char ch : s1){
            mp[ch] = 1;
        }
        for(char ch : s2){
            mp[ch] = 2;
        }
        vector<string> res;
        for(string word : words){
            int sz = word.size();
            bool canPrint = true;
            for(int i = 1; i < sz; i++){
                if(mp[word[i]] != mp[word[i-1]]){
                    canPrint = false;
                    break;
                }
            }
            if(canPrint){
                res.push_back(word);
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10594235.html

时间: 2024-08-02 07:59:35

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

LeetCode 键盘行&lt;四&gt;

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

Leetcode#500. Keyboard Row(键盘行)

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

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

Oracle 行链接(Row chaining) 与行迁移(Row Migration)

场景:如果VarChar和VarChar2更经常修改,且修改的数据长度每次都不一样,这会引起“行迁移”现象 概念: 行链接(Row chaining) 与行迁移(Row Migration)当一行的数据过长而不能插入一个单个数据块中时,可能发生两种事情:行链接(row chaining)或行迁移(row migration). 行链接当第一次插入行时,由于行太长而不能容纳在一个数据块中时,就会发生行链接.在这种情况下,oracle会使用与该块链接的一块或多块数据块来容纳该行的数据.行连接经常在插

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

[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"]