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

LeetCode-500. Keyboard Row

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

标签:arraylist   using   one   for   word   cas   leetcode   first   length   

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

标签:arraylist   using   one   for   word   cas   leetcode   first   length   

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

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