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.

分析:

这道题就是要求判断输入的单词是否是键盘上处于同一行的字母输入的。刚开始看到这个题的时候,脑子里第一个思路是,我把每一行的字母存到一个数组里,每行字母对应一个数组,这样会有三个数组比如A=[‘Q‘,‘W‘,‘E‘,‘R‘...] , B=[‘A‘,‘S‘,‘D‘,‘F‘..], C=[‘Z‘,‘X‘,‘C‘,‘V‘..], 然后输入一个待判断的单词,然后我逐个取这个单词的charcode,判断第一个charcode命中哪一个数组,那么后续的判断就以那个数组为参考,遍历charcode判断charcode是否在这个数组就可以了。

然后么,想了下会不会有其他方法呢,用正则吧,思路一样,不过不用写那么多代码。这里参考代码写一下:

function test(word){
 return /(^[QWERTYUIOPqwertyuiop]+$|^[ASDFGHJKLasdfghjkl]+$|^[ZXCVBNMzxcvbnm]+$)/.test(word);
}
test(‘wefgv‘);
时间: 2024-10-21 18: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 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

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