zoukankan      html  css  js  c++  java
  • 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是否在这个数组就可以了。

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

    var findWords = function(words) {
         let result = [];
         words.forEach((word) => {
             if(/(^[QWERTYUIOPqwertyuiop]+$|^[ASDFGHJKLasdfghjkl]+$|^[ZXCVBNMzxcvbnm]+$)/.test(word)) {
                 result.push(word);
             }
         });
         return result;
    };
  • 相关阅读:
    Codeforces 1316B String Modification
    Codeforces 1305C Kuroni and Impossible Calculation
    Codeforces 1305B Kuroni and Simple Strings
    Codeforces 1321D Navigation System
    Codeforces 1321C Remove Adjacent
    Codeforces 1321B Journey Planning
    Operating systems Chapter 6
    Operating systems Chapter 5
    Abandoned country HDU
    Computer HDU
  • 原文地址:https://www.cnblogs.com/gogolee/p/6648919.html
Copyright © 2011-2022 走看看