public class Solution {
/*
* @param nums1: an integer array
* @param nums2: an integer array
* @return: an integer array
*/
public int[] intersection(int[] nums1, int[] nums2) {
// write your code here
if(nums1.length == 0 || nums2.length == 0){
return new int[]{};
}
Arrays.sort(nums1);
Arrays.sort(nums2);
int index1 =0;
int index2 = 0;
List<Integer> list = new ArrayList<Integer>();
List<Integer> list2 = new ArrayList<Integer>();
while(index1<nums1.length && index2<nums2.length){
if(nums1[index1] == nums2[index2]){
list.add(nums1[index1]);
index1++;
index2++;
}else if(nums1[index1] < nums2[index2]){
index1++;
}else{
index2++;
}
}
int []res = new int [list.size()];
for (int i = 0; i<list.size();i++){
res[i] = list.get(i);
}
return res;
}
};
lintcode 548 两数组的交2
最新推荐文章于 2021-08-16 09:53:12 发布