List list = new ArrayList(0);
for (...)
{
list.add(...);
}
..[] = list.toArray(...);
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/86ff05d4014cf8b005546d491fd0203a.png)
class Solution {
public String[] findWords(String[] words) {
String s1 = "qwertyuiopQWERTYUIOP";
String s2 = "asdfghjklASDFGHJKL";
String s3 = "zxcvbnmZXCVBNM";
List<String> list = new ArrayList<>();
for(String word : words ){
int len = word.length();
int n1 = 0, n2 = 0, n3 = 0;
for(int i = 0; i < len; i++){
if(s1.contains(word.charAt(i)+"")) n1++;
else if(s2.contains(word.charAt(i)+"")) n2++;
else n3++;
}
if(n1 == len || n2 == len || n3 == len) list.add(word);
}
return list.toArray(new String[list.size()]);
}
}