再说一遍,能用哈希集合做的,哈希数组也有可能做出来!
LeetCode链接:349. 两个数组的交集
1.题目描述
给定两个数组 nums1
和 nums2
,返回 它们的 交集。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
示例 1:
1 2
| 输入:nums1 = , nums2 = 输出:
|
示例 2:
1 2 3
| 输入:nums1 = , nums2 = 输出: 解释: 也是可通过的
|
提示:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
2.题解
2.1 直接法(暴力解法)-哈希集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set1 = new HashSet<Integer>(); Set<Integer> set2 = new HashSet<Integer>();
for (int num : nums1) { set1.add(num); }
for (int num : nums2) { set2.add(num); }
return getIntersection(set1, set2); }
public int[] getIntersection(Set<Integer> set1, Set<Integer> set2) { if (set1.size() > set2.size()) { return getIntersection(set2, set1); }
Set<Integer> intersectionSet = new HashSet<Integer>();
for (int num : set1) { if (set2.contains(num)) { intersectionSet.add(num); } }
int[] intersection = new int[intersectionSet.size()]; int index = 0; for (int num : intersectionSet) { intersection[index++] = num; }
return intersection; } }
|
2.2 哈希集合优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums1.length; i++) { set.add(nums1[i]); }
Set<Integer> resultSet = new HashSet<>(); for (int i = 0; i < nums2.length; i++) { if (set.contains(nums2[i])) resultSet.add(nums2[i]); } int[] result = new int[resultSet.size()]; int i = 0; for (int num : resultSet) { result[i++] = num; } return result; } }
|
2.3 哈希数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { public int[] intersection(int[] nums1, int[] nums2) { int[] arr1 = new int[1001]; int[] arr2 = new int[1001];
for (int i = 0; i < nums1.length; i++) { arr1[nums1[i]]++; }
int count = 0; for (int i = 0; i < nums2.length; i++) { if (arr1[nums2[i]] != 0 && arr2[nums2[i]] == 0) { count++; arr2[nums2[i]]++; } }
int[] result = new int[count]; int index = 0; for (int i = 0; i < arr2.length; i++) { if (arr2[i] != 0) result[index++] = i; }
return result; } }
|
2.4 排序+双指针
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| class Solution { public int[] intersection(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); int length1 = nums1.length, length2 = nums2.length; int[] intersection = new int[length1 + length2]; int index = 0; int index1 = 0, index2 = 0; while (index1 < length1 && index2 < length2) { int num1 = nums1[index1], num2 = nums2[index2]; if (num1 == num2) { if (index == 0 || num1 != intersection[index - 1]) { intersection[index++] = num1; } index1++; index2++; } else if (num1 < num2) { index1++; } else { index2++; } } return Arrays.copyOfRange(intersection, 0, index); } }
|
2.5 JS语法糖
1 2 3 4 5 6 7 8 9 10 11 12 13
|
var intersection = function(nums1, nums2) { let set1 = new Set(nums1); let set2 = new Set(nums2);
return [...set1].filter(num => set2.has(num)); };
|