Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> re=new ArrayList<Integer>();
if(nums1.length==0||nums2.length==0){
return new int[0];
}
Arrays.sort(nums1);
Arrays.sort(nums2);
int i=0;
int j=0;
while(i<nums1.length&&j<nums2.length){
if(nums1[i]>nums2[j]){
j++;
}
else if(nums1[i]<nums2[j]){
i++;
}
else{
re.add(nums1[i]);
i++;
j++;
}
}
int[] out=new int[re.size()];
int inx=0;
for(Integer n:re)
out[inx++]=n;
return out;
}
}