Skip to content

Commit 2c05afa

Browse files
committed
三刷90
1 parent 89c4348 commit 2c05afa

File tree

6 files changed

+77
-27
lines changed

6 files changed

+77
-27
lines changed

Diff for: docs/0000-00-note.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ image::images/quick-sort-01.gif[{image_attr}]
150150
.. Sliding Window Median (hard)
151151
.. Maximize Capital (hard)
152152
.. Problem Challenge 1: Next Interval (hard)
153-
. xref:0000-24-subsets.adoc[Subsets 子集]
153+
. xref:0000-25-subsets.adoc[Subsets 子集]
154154
.. Subsets (easy)
155155
.. Subsets With Duplicates (easy)
156156
.. Permutations (medium)

Diff for: docs/0000-24-subsets.adoc renamed to docs/0000-25-subsets.adoc

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[#0000-24-subsets]
1+
[#0000-25-subsets]
22
= Subsets 子集
33

44
一般都是使用多重DFS
@@ -29,10 +29,3 @@
2929
. xref:0241-different-ways-to-add-parentheses.adoc[241. Different Ways to Add Parentheses]
3030
. xref:0320-generalized-abbreviation.adoc[320. Generalized Abbreviation]
3131
. xref:0784-letter-case-permutation.adoc[784. Letter Case Permutation]
32-
33-
34-
String Permutations by changing case (medium)
35-
36-
Balanced Parentheses (hard)
37-
38-
Unique Generalized Abbreviations (hard)

Diff for: docs/0090-subsets-ii.adoc

+31-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
[#0090-subsets-ii]
2-
= 90. Subsets II
2+
= 90. 子集 II
33

4-
{leetcode}/problems/subsets-ii/[LeetCode - Subsets II^]
4+
https://leetcode.cn/problems/subsets-ii/[LeetCode - 90. 子集 II ^]
55

6-
Given a collection of integers that might contain duplicates, `nums`, return all possible subsets (the power set).
6+
给你一个整数数组 `nums`,其中可能包含重复元素,请你返回该数组所有可能的 子集(幂集)。
77

8-
*Note:* The solution set must not contain duplicate subsets.
8+
解集 *不能* 包含重复的子集。返回的解集中,子集可以按 *任意顺序* 排列。
99

10-
.Example:
11-
----
12-
Input: [1,2,2]
13-
Output:
14-
[
15-
[2],
16-
[1],
17-
[1,2,2],
18-
[2,2],
19-
[1,2],
20-
[]
21-
]
22-
----
10+
*示例 1:*
11+
12+
....
13+
输入:nums = [1,2,2]
14+
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]
15+
....
16+
17+
*示例 2:*
18+
19+
....
20+
输入:nums = [0]
21+
输出:[[],[0]]
22+
....
23+
24+
*提示:*
25+
26+
* `+1 <= nums.length <= 10+`
27+
* `+-10 <= nums[i] <= 10+`
2328
2429
== 解题分析
2530

@@ -56,6 +61,15 @@ include::{sourcedir}/_0090_SubsetsII.java[tag=answer]
5661
include::{sourcedir}/_0090_SubsetsII_2.java[tag=answer]
5762
----
5863
--
64+
65+
三刷::
66+
+
67+
--
68+
[{java_src_attr}]
69+
----
70+
include::{sourcedir}/_0090_SubsetsII_3.java[tag=answer]
71+
----
72+
--
5973
====
6074

6175
== 参考资料

Diff for: docs/index.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ include::0000-23-transform-and-conquer.adoc[leveloffset=+1]
7070

7171
include::0000-24-backtrack.adoc[leveloffset=+1]
7272

73-
include::0000-24-subsets.adoc[leveloffset=+1]
73+
include::0000-25-subsets.adoc[leveloffset=+1]
7474

7575
include::0000-25-greedy.adoc[leveloffset=+1]
7676

Diff for: logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ endif::[]
205205
|{doc_base_url}/0322-coin-change.adoc[题解]
206206
|⭕️ 动态规划,完全背包问题
207207

208+
|{counter:codes2503}
209+
|{leetcode_base_url}/subsets-ii/[90. 子集 II]
210+
|{doc_base_url}/0090-subsets-ii.adoc[题解]
211+
|⭕️ 子集,需要注意重复元素的处理。现在用 `Set` 记录已添加子集的方案还可以再优化。
212+
208213
|===
209214

210215
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.*;
4+
5+
public class _0090_SubsetsII_3 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-04-09 18:04:54
10+
*/
11+
public List<List<Integer>> subsetsWithDup(int[] nums) {
12+
List<List<Integer>> result = new ArrayList<>();
13+
Arrays.sort(nums);
14+
result.add(new ArrayList<>());
15+
// 使用 Set 记录已添加的子集,这个方案还需要再优化
16+
Set<String> existed = new HashSet<>();
17+
for (int i = 0; i < nums.length; i++) {
18+
int num = nums[i];
19+
int size = result.size();
20+
for (int j = 0; j < size; j++) {
21+
List<Integer> subset = new ArrayList<>(result.get(j));
22+
subset.add(num);
23+
StringBuilder sb = new StringBuilder(nums.length);
24+
for (Integer ai : subset) {
25+
sb.append(ai);
26+
}
27+
if (existed.add(sb.toString())) {
28+
result.add(subset);
29+
}
30+
}
31+
}
32+
return result;
33+
}
34+
// end::answer[]
35+
public static void main(String[] args) {
36+
new _0090_SubsetsII_3().subsetsWithDup(new int[]{1, 2, 2});
37+
}
38+
}

0 commit comments

Comments
 (0)