标签:str string lin inf lis for 注意 字母 .com
示例:
输入: ["Hello", "Alaska", "Dad", "Peace"]
输出: ["Alaska", "Dad"]
注意:
你可以重复使用键盘上同一字符。
你可以假设输入的字符串将只包含字母。
class Solution {
public String[] findWords(String[] words) {
List<String> list = new LinkedList<>();
String q = "qwertyuiopQWERTYUIOP";
String a = "asdfghjklASDFGHJKL";
String z = "zxcvbnmZXCVBNM";
int i,j;
for(i=0;i<words.length;i++) {
int len = words[i].length();
int one=0,two=0,three=0;//单词字母所在的行数
for(j=0;j<len;j++) {
String s = words[i].substring(j,j+1);
if(q.contains(s)) {
one++;
}else if(a.contains(s)) {
two++;
}else if(z.contains(s)) {
three++;
}
}
// 如果单词每个字母都在同一行,则添加进list
if(one == len || two == len || three == len) {
list.add(words[i]);
}
}
return list.toArray(new String[list.size()]);
}
}
标签:str string lin inf lis for 注意 字母 .com
原文地址:https://www.cnblogs.com/lick468/p/10669643.html