[LC] 349. Intersection of Two Arrays
程序员文章站
2022-07-15 10:46:37
...
这题真的很没有营养,一个HashSet就能解决的问题。如果真的要变个花样不用任何空间,排个序也可以,真的没有什么好思考的,直接给代码就好了。
public int[] intersection(int[] nums1, int[] nums2) {
HashSet<Integer> set = new HashSet<>();
for (int i : nums1) set.add(i);
HashSet<Integer> resSet = new HashSet<>();
for (int i : nums2) {
if (set.contains(i)) {
resSet.add(i);
}
}
int[] res = new int[resSet.size()];
int cnt = 0;
for (Integer i : resSet) {
res[cnt] = i;
cnt++;
}
return res;
}
推荐阅读
-
[LC] 349. Intersection of Two Arrays
-
349. Intersection of Two Arrays
-
LeetCode刷题笔记(Intersection of Two Arrays II)
-
LeetCode 350. Intersection of Two Arrays II
-
leetcode 349. Intersection of Two Arrays(C语言)10
-
LeetCode 350. Intersection of Two Arrays II
-
Binary Search:349. Intersection of Two Arrays
-
LeetCode 349. Intersection of Two Arrays
-
LeetCode-349. Intersection of Two Arrays
-
【LeetCode】349. Intersection of Two Arrays