首页 > 代码库 > leetcode Keyboard Row500 Java

leetcode Keyboard Row500 Java

 1 public class Solution {
 2     public String[] findWords(String[] words) {
 3         List<String> oneRowWords = new ArrayList<String>();
 4         String[] keyboard = {"qwertyuiop","asdfghjkl","zxcvbnm"};
 5         for(String word : words) {
 6             String realWord = word;
 7             word = word.toLowerCase();//每个字母变为小写
 8             char[] strBit = word.toCharArray();
 9             int count = 0;
10             for(char ch : strBit) {
11                 if(keyboard[0].indexOf(strBit[0]) != -1) {//第一个字母在第一排
12                     if(keyboard[0].indexOf(ch) == -1) {//其他字母必须也在第一排 否则跳过
13                         break;
14                     }
15                 }else if(keyboard[1].indexOf(strBit[0]) != -1) {//第一个字母在第二排
16                     if(keyboard[1].indexOf(ch) == -1) {
17                         break;
18                     }
19                 }else if(keyboard[2].indexOf(strBit[0]) != -1) {//第一个字母在第三排
20                     if(keyboard[2].indexOf(ch) == -1) {
21                         break;
22                     }
23                 }
24                 count ++;
25             }
26             if(count == strBit.length) {
27                 oneRowWords.add(realWord);
28             }
29         }
30         String[] oneRowWordsArray = new String[oneRowWords.size()];
31         for(int i=0; i<oneRowWords.size(); i++){
32             oneRowWordsArray[i] = oneRowWords.get(i);
33         }
34         
35         return oneRowWordsArray;
36     }
37 }

 

leetcode Keyboard Row500 Java