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:
1 | Input: ["Hello", "Alaska", "Dad", "Peace"] |
Note:
- You may use one character in the keyboard more than once.
- You may assume the input string will only contain letters of alphabet.
理解
水题,题目的意思就是那些单词可以只用一行上的字母来生成.先以单词的第一个字母来确定属于哪一行,然后逐个判断后面的字母是不是这一行的就可以了.
代码
1 | class Solution { |
其他的解法
Discuss里面也有很多很简短的代码的解法,基本上都是用到了正则表达式和过滤的结合,这里贴一个Python版的:
1 | def findWords(self, words): |
一行就解决了这个问题,正则还是强大啊~