码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode. 500. Keyboard Row

时间:2017-03-30 22:28:51      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:...   block   tco   string   sum   letter   分析   第一个   input   

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‘);

 

LeetCode. 500. Keyboard Row

标签:...   block   tco   string   sum   letter   分析   第一个   input   

原文地址:http://www.cnblogs.com/gogolee/p/6648919.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!