50,535
社区成员
发帖
与我相关
我的任务
分享
class Solution {
//参数strs1数组1,strs2数组2,a数组1中指定位置,b与数组1中指定值组合的值在数组2中的下标
public List<String> longestCommonPrefix(String[] strs1, String[] strs2, int a, int b) {
List<String> result = new LinkedList<>();
if (strs1.length==0 || strs2.length==0){//判断有空数组时直接返回空集合
return result;
}
for (int i=0;i<strs1.length;i++){//循环数组1
if (i==a){//判断数组1中指定值仅与数组2中指定值组合
result.add(strs1[i]+strs2[b]);
}else{//数组1中其余值与数组2中的值进行组合
for (int j=0;j<strs2.length;j++){
result.add(strs1[i]+strs2[j]);
}
}
}
return result;
}
public static void main(String[] args) {
Solution solution=new Solution();
String[] aaa=new String[]{"1","2","3","4"};//数组1
String[] bbb=new String[]{"a","b","c","d"};//数组2
List str = solution.longestCommonPrefix(aaa,bbb,0,0);
System.out.println(str);
}
}