代碼隨想錄-刷題筆記
349. 兩個數組的交集 - 力扣(LeetCode)
內容:
集合的使用 , 重復的數剔除掉,剩下的即為交集,最后加入數組即可。
class Solution {public int[] intersection(int[] nums1, int[] nums2) {Set<Integer> result = new HashSet<>();Map<Integer,Integer> map = new HashMap<>();for(int i : nums1) {map.put(i,map.getOrDefault(i, 0) + 1);}for(int j : nums2) {if(map.getOrDefault(j, 0) != 0) {result.add(j);}}return result.stream().mapToInt(Integer::intValue).toArray();}
}
總結:
集合入門.