leetcode Keyboard Row500 Java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode. 500. Keyboard Row

Leetcode500 Keyboard Row

leetcode Keyboard Row500 Java

LeetCode 500. Keyboard Row (键盘行)

leetcode 500. 键盘行(Keyboard Row)

Leetcode#500. Keyboard Row(键盘行)