Skip to content

Commit d9cb5d3

Browse files
committed
二刷350
1 parent 72767fe commit d9cb5d3

File tree

3 files changed

+79
-26
lines changed

3 files changed

+79
-26
lines changed
+42-26
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,64 @@
11
[#0350-intersection-of-two-arrays-ii]
2-
= 350. Intersection of Two Arrays II
2+
= 350. 两个数组的交集 II
33

4-
{leetcode}/problems/intersection-of-two-arrays-ii/[LeetCode - Intersection of Two Arrays II^]
4+
https://leetcode.cn/problems/intersection-of-two-arrays-ii/[LeetCode - 350. 两个数组的交集 II ^]
55

6-
Given two arrays, write a function to compute their intersection.
6+
给你两个整数数组 `nums1``nums2`,请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值)。可以不考虑输出结果的顺序。
77

8-
*Example 1:*
9-
10-
[subs="verbatim,quotes,macros"]
11-
----
12-
*Input:* nums1 = [1,2,2,1], nums2 = [2,2]
13-
*Output:* [2,2]
14-
----
15-
16-
17-
*Example 2:*
18-
19-
[subs="verbatim,quotes,macros"]
20-
----
21-
*Input:* nums1 = [4,9,5], nums2 = [9,4,9,8,4]
22-
*Output:* [4,9]
23-
----
8+
*示例 1:*
249

10+
....
11+
输入:nums1 = [1,2,2,1], nums2 = [2,2]
12+
输出:[2,2]
13+
....
2514

26-
*Note:*
15+
*示例 2:*
2716

17+
....
18+
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
19+
输出:[4,9]
20+
....
2821

29-
* Each element in the result should appear as many times as it shows in both arrays.
30-
* The result can be in any order.
3122

23+
*提示:*
3224

33-
*Follow up:*
25+
* `+1 <= nums1.length, nums2.length <= 1000+`
26+
* `+0 <= nums1[i], nums2[i] <= 1000+`
3427
28+
**进阶*:*
3529
36-
* What if the given array is already sorted? How would you optimize your algorithm?
37-
* What if _nums1_'s size is small compared to _nums2_'s size? Which algorithm is better?
38-
* What if elements of _nums2_ are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
30+
* 如果给定的数组已经排好序呢?你将如何优化你的算法?
31+
* 如果 `nums1` 的大小比 `nums2` 小,哪种方法更优?
32+
* 如果 `nums2` 的元素存储在磁盘上,内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?
3933
4034
35+
== 思路分析
4136
37+
最初思路是分别对两个数组遍历,使用 `Map` 来统计每个数字出现的次数,然后求交集。其实,只需要对一个数组统计每个数字出现的次数即可,在另外一个数组遍历时,根据次数来判断是否重复。可以介绍一个“取交集”的遍历。
4238
4339
[[src-0350]]
40+
[tabs]
41+
====
42+
一刷::
43+
+
44+
--
4445
[{java_src_attr}]
4546
----
4647
include::{sourcedir}/_0350_IntersectionOfTwoArraysIi.java[tag=answer]
4748
----
49+
--
50+
51+
二刷::
52+
+
53+
--
54+
[{java_src_attr}]
55+
----
56+
include::{sourcedir}/_0350_IntersectionOfTwoArraysIi_2.java[tag=answer]
57+
----
58+
--
59+
====
60+
61+
62+
== 参考资料
63+
4864

logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,11 @@ endif::[]
663663
|{doc_base_url}/1769-minimum-number-of-operations-to-move-all-balls-to-each-box.adoc[题解]
664664
|✅ 最简单的解法双重循环。取巧的解法是,利用“前缀和”的思路,分别计算当前节点左右两侧的操作次数,然后再相加。
665665

666+
|{counter:codes2503}
667+
|{leetcode_base_url}/intersection-of-two-arrays-ii/[350. 两个数组的交集 II^]
668+
|{doc_base_url}/0350-intersection-of-two-arrays-ii.adoc[题解]
669+
|✅ 最初思路是对两个数组统计次数。其实只需要对一个数组统计次数,另外一个数组遍历时就可以求交集了。
670+
666671
|===
667672

668673
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class _0350_IntersectionOfTwoArraysIi_2 {
9+
// tag::answer[]
10+
11+
/**
12+
* @author D瓜哥 · https://www.diguage.com
13+
* @since 2025-05-12 16:40:45
14+
*/
15+
public int[] intersect(int[] nums1, int[] nums2) {
16+
Map<Integer, Integer> numToCnt = new HashMap<>();
17+
for (int i : nums1) {
18+
Integer cnt = numToCnt.getOrDefault(i, 0);
19+
numToCnt.put(i, cnt + 1);
20+
}
21+
List<Integer> result = new ArrayList<>();
22+
for (int i : nums2) {
23+
Integer cnt = numToCnt.getOrDefault(i, 0);
24+
if (cnt > 0) {
25+
result.add(i);
26+
numToCnt.put(i, cnt - 1);
27+
}
28+
}
29+
return result.stream().mapToInt(i -> i).toArray();
30+
}
31+
// end::answer[]
32+
}

0 commit comments

Comments
 (0)