public static int[] intersection(int[] nums1, int[] nums2) {
int[] shortI = nums1;
int[] longI = nums2;
if(shortI.length > longI.length) {
shortI = nums2;
longI = nums1;
}
int count = 0;
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i < shortI.length; i++) {
for(int j = 0; j < longI.length; j++) {
if(shortI[i] == longI[j]) {
set.add(shortI[i]);
break;
}
}
}
int[] result = new int[set.size()];
for(Integer i : set) {
result[count++] = i;
}
return result;
}
349. Intersection of Two Arrays
最新推荐文章于 2024-08-06 17:50:14 发布