标签:list ret rar lis line index iss code dex
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。
public String[] findWords(String[] words) {
ArrayList<String> res = new ArrayList<>();
for (String word : words) {
if (isSameLine(word)) {
res.add(word);
}
}
return res.toArray(new String[0]);
}
private boolean isSameLine(String word) {
char st = word.charAt(0);
String line = whichLine(st);
for (char c : word.toCharArray()) {
if (!line.contains(String.valueOf(c))) {
return false;
}
}
return true;
}
private String whichLine(char st) {
String l1 = "QWERTYUIOPqwertyuiop";
String l2 = "ASDFGHJKLasdfghjkl";
String l3 = "ZXCVBNMzxcvbnm";
if (l1.contains(String.valueOf(st))) {
return l1;
} else if (l2.contains(String.valueOf(st))) {
return l2;
} else {
return l3;
}
}
// 改成indexOf函数
public String[] findWords(String[] words) {
ArrayList<String> res = new ArrayList<>();
for (String word : words) {
if (isSameLine(word)) {
res.add(word);
}
}
return res.toArray(new String[0]);
}
private boolean isSameLine(String word) {
char st = word.charAt(0);
String line = whichLine(st);
for (char c : word.toCharArray()) {
if (line.indexOf(c) < 0) {
return false;
}
}
return true;
}
private String whichLine(char st) {
String l1 = "QWERTYUIOPqwertyuiop";
String l2 = "ASDFGHJKLasdfghjkl";
String l3 = "ZXCVBNMzxcvbnm";
if (l1.indexOf(st) >= 0) {
return l1;
} else if (l2.indexOf(st) >= 0) {
return l2;
} else {
return l3;
}
}
标签:list ret rar lis line index iss code dex
原文地址:https://www.cnblogs.com/angelica-duhurica/p/12227294.html