首页 > 其他 > 详细

LeetCode-500. Keyboard Row

时间:2017-02-11 19:00:08      阅读:574      评论:0      收藏:0      [点我收藏+]

Given a List of words, return the words that can be typed using letters of alphabet on only one row‘s of American keyboard

public class Solution {
    public String[] findWords(String[] words) {
        if (words == null)
            return new String[0];
        String[] r = {"qwertyuiop", "asdfghjkl", "zxcvbnm"};
        List<String> rets = new ArrayList<String>();
        for (String ws : words) {
            String w = ws.toLowerCase();
            char first = w.charAt(0);
            int in = 0;
            while (in < 3) {
                if (r[in].contains(first+""))
                    break;
                in ++;
            }
            boolean all = true;
            for (int i=1; i<w.length(); i++) {
                if (!r[in].contains(w.charAt(i)+"")) {
                    all = false;
                    break;
                }
            }
            if (all) {
                rets.add(ws);
            }
        }
        return rets.toArray(new String[0]);
    }
    
   
}

 

LeetCode-500. Keyboard Row

原文:http://www.cnblogs.com/wxisme/p/6389482.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!