思路:
这道题目,主要要学会使用一种哈希数据结构:unordered_set,这个数据结构可以解决很多类似的问题。
注意题目特意说明:输出结果中的每个元素一定是唯一的,也就是说输出的结果的去重的, 同时可以不考虑输出结果的顺序
使用数组来做哈希的题目,是因为题目都限制了数值的大小。
class Solution {public int[] intersection(int[] nums1, int[] nums2) {if (nums1==null||nums1.length==0||nums2==null||nums2.length==0){return new int[0];}Set<Integer> newset=new HashSet<>();Set<Integer> resset=new HashSet<>();for(int i:nums1){newset.add(i);}for(int i:nums2){if(newset.contains(i)){resset.add(i);}}int[] arr=new int[resset.size()];int j=0;for(int i:resset){arr[j++]=i;}return arr;}
}