关于leetcode:leetcode之键盘行

本文次要记录一下leetcode之键盘行

题目

给定一个单词列表,只返回能够应用在键盘同一行的字母打印进去的单词。键盘如下图所示。

![](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/10/12/keyboard.png)

示例:

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

留神:

你能够重复使用键盘上同一字符。
你能够假如输出的字符串将只蕴含字母。

起源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/keyboard-row
著作权归领扣网络所有。商业转载请分割官网受权,非商业转载请注明出处。

题解

class Solution {

    public String[] findWords(String[] words) {
        List<String> result = new ArrayList<>();
        for (String word : words) {
            String str = word.toLowerCase();
            if (str.matches("[qwertyuiop]+") || str.matches("[asdfghjkl]+") || str.matches("[zxcvbnm]+")) {
                result.add(word);
            }
        }
        return result.toArray(new String[result.size()]);
    }
}

小结

这里利用java的String的matches办法来进行正则匹配,将满足条件的增加到后果集中。

doc

  • 键盘行

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理