public class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
HashMap<Integer,Integer> map=new HashMap<>();
ArrayList<Integer> arr=new ArrayList<>();
for(int i=0;i<nums1.length;i++){
if(map.containsKey(nums1[i])){
map.put(nums1[i],map.get(nums1[i])+1);
}else map.put(nums1[i],1);
}
for(int i=0;i<nums2.length;i++){
if(map.containsKey(nums2[i])&&map.get(nums2[i])>0){
arr.add(nums2[i]);
map.put(nums2[i],map.get(nums2[i])-1);
}
}
int k=0;
int[] res=new int[arr.size()];
for(Integer num: arr){
res[k++]=num;
}
return res;
}
}
350. Intersection of Two Arrays II
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- Intersection of Two Arrays IIGiven two arrays, write a fu...
- Given two arrays, write a function to compute their inter...
- Given two arrays, write a function to compute their inter...
- Given two arrays, write a function to compute their inter...