题目描述:
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
输入:
[“Hello”, “Alaska”, “Dad”, “Peace”]
输出:
[“Alaska”, “Dad”]
题意:
题目描述
题解:
直接搞
代码:
class Solution {
public static String[] findWords(String[] words) {
String a = "QWERTYUIOPqwertyuiop";
String b = "ASDFGHJKLasdfghjkl";
String c = "ZXCVBNMzxcvbnm";
List<String> list = new ArrayList<>();
int len = words.length;
for(int i = 0; i < len; i ++){
char [] ch = words[i].toCharArray();
String op = String.valueOf(ch[0]);
int t = 0;
if(a.contains(op)) t = 1;
else if(b.contains(op)) t = 2;
else t = 3;
boolean flag = true;
for(int j = 1; j < ch.length; j ++){
String ops = String.valueOf(ch[j]);
int ts = 0;
if(a.contains(ops)) ts = 1;
if(b.contains(ops)) ts = 2;
else if(c.contains(ops)) ts = 3;
if(ts != t){
flag = false;
break;
}
}
if(flag) list.add(words[i]);
}
String [] ans = new String [list.size()];
for(int i = 0; i < list.size(); i ++){
ans[i] = list.get(i);
}
return ans;
}
}