class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if(nums1 ==null||nums2 ==null){
return null;
}
HashMap<Integer , Integer> map = new HashMap<>();
for(int i = 0; i < nums1.length; i++){
map.put(nums1[i], map.getOrDefault(nums1[i],0) + 1);
}
LinkedList<Integer> list = new LinkedList<Integer>();
for (int j= 0; j < nums2.length; j++) {
Integer num=map .get(nums2 [j]) ;
if(num!=null){
list .add(nums2[j]);
map .remove(nums2[j]) ;
}
}
int count = list.size();
int[] aux = new int[count];
for(int i = 0; i < count; i++){
aux[i] = list.poll();
}
return aux;
}
}
求唯一交集
最新推荐文章于 2025-02-14 23:25:03 发布