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.

 

American keyboard

 

Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
题目含义:给出n个字符串,从而判断每个字符串中的字符石头来自美式键盘上的同一行,若来自同一行,返回该string

 1     public String[] findWords(String[] words) {
 2        List<String> okWorkd = new ArrayList<>();
 3        String row1 = "qwertyuiop";
 4        String row2 = "asdfghjkl";
 5        String row3 = "zxcvbnm";
 6         for (String word : words) {
 7             if (word.isEmpty()) continue;
 8             boolean row1OK = true;
 9             boolean row2OK = true;
10             boolean row3OK = true;
11             for (int i=0;i<word.length();i++)
12             {
13                 if (!row1.contains(String.valueOf(word.charAt(i)).toLowerCase()))
14                 {
15                     row1OK = false;
16                 }
17 
18                 if (!row2.contains(String.valueOf(word.charAt(i)).toLowerCase()))
19                 {
20                     row2OK = false;
21                 }
22 
23                 if (!row3.contains(String.valueOf(word.charAt(i)).toLowerCase()))
24                 {
25                     row3OK = false;
26                 }
27             }
28             if (row1OK || row2OK || row3OK) okWorkd.add(word);
29         }
30 
31         String[] reuslt = new String[okWorkd.size()];
32         okWorkd.toArray(reuslt);
33         return reuslt;        
34     }

 

posted @ 2017-10-23 20:05  daniel456  阅读(127)  评论(0编辑  收藏  举报