Skip to content

Commit de80d21

Browse files
committed
四刷40
1 parent f7a18ac commit de80d21

File tree

5 files changed

+99
-23
lines changed

5 files changed

+99
-23
lines changed

docs/0040-combination-sum-ii.adoc

+44-23
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
[#0040-combination-sum-ii]
2-
= 40. Combination Sum II
2+
= 40. 组合总和 II
33

4-
{leetcode}/problems/combination-sum-ii/[LeetCode - Combination Sum II^]
4+
https://leetcode.cn/problems/combination-sum-ii/[LeetCode - 40. 组合总和 II ^]
55

6-
Given a collection of candidate numbers (`candidates`) and a target number (`target`), find all unique combinations in `candidates` where the candidate numbers sums to `target`.
6+
给定一个候选人编号的集合 `candidates` 和一个目标数 `target` ,找出 `candidates` 中所有可以使数字和为 `target` 的组合。
77

8-
Each number in `candidates` may only be used once in the combination.
8+
`candidates` 中的每个数字在每个组合中只能使用 *一次*
99

10-
*Note:*
10+
**注意:**解集不能包含重复的组合。
1111

12-
* All numbers (including target) will be positive integers.
13-
* The solution set must not contain duplicate combinations.
12+
*示例 1:*
1413

15-
.Example 1:
16-
[source]
17-
----
18-
Input: candidates = [10,1,2,7,6,1,5], target = 8,
19-
A solution set is:
14+
....
15+
输入: candidates = [10,1,2,7,6,1,5], target = 8,
16+
输出:
2017
[
21-
[1, 7],
22-
[1, 2, 5],
23-
[2, 6],
24-
[1, 1, 6]
18+
[1,1,6],
19+
[1,2,5],
20+
[1,7],
21+
[2,6]
2522
]
26-
----
23+
....
2724

28-
.Example 2:
29-
[source]
30-
----
31-
Input: candidates = [2,5,2,1,2], target = 5,
32-
A solution set is:
25+
*示例 2:*
26+
27+
....
28+
输入: candidates = [2,5,2,1,2], target = 5,
29+
输出:
3330
[
3431
[1,2,2],
3532
[5]
3633
]
37-
----
34+
....
35+
36+
37+
*提示:*
38+
39+
* `+1 <= candidates.length <= 100+`
40+
* `+1 <= candidates[i] <= 50+`
41+
* `+1 <= target <= 30+`
42+
3843
3944
== 思路分析
4045

@@ -80,8 +85,24 @@ include::{sourcedir}/_0040_CombinationSumII_2.java[tag=answer]
8085
include::{sourcedir}/_0040_CombinationSumII_3.java[tag=answer]
8186
----
8287
--
88+
89+
四刷::
90+
+
91+
--
92+
[{java_src_attr}]
93+
----
94+
include::{sourcedir}/_0040_CombinationSumIi_4.java[tag=answer]
95+
----
96+
--
8397
====
8498

99+
100+
== 思考题
101+
102+
如果一个既存在重复元素,同一个元素又可以重复使用,该怎么做组合总和?
103+
104+
其实,不能这样搞。没办法区分是同一个元素,还是重复的元素。
105+
85106
== 参考资料
86107

87108
. https://leetcode.cn/problems/combination-sum-ii/solutions/407850/zu-he-zong-he-ii-by-leetcode-solution/[40. 组合总和 II - 官方题解^]

docs/images/0040-10.png

301 KB
Loading

docs/images/0040-11.png

166 KB
Loading

logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,11 @@ endif::[]
493493
|{doc_base_url}/0039-combination-sum.adoc[题解]
494494
|✅ 回溯
495495

496+
|{counter:codes2503}
497+
|{leetcode_base_url}/combination-sum-ii/[40. 组合总和 II^]
498+
|{doc_base_url}/0040-combination-sum-ii.adoc[题解]
499+
|✅ 回溯。注意同层剪枝的技巧。
500+
496501
|===
497502

498503
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class _0040_CombinationSumIi_4 {
8+
// tag::answer[]
9+
10+
/**
11+
* @author D瓜哥 · https://www.diguage.com
12+
* @since 2024-09-16 17:31:07
13+
*/
14+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
15+
Arrays.sort(candidates);
16+
List<List<Integer>> result = new ArrayList<>();
17+
List<Integer> path = new ArrayList<>();
18+
backtrack(candidates, target, result, path, 0);
19+
return result;
20+
}
21+
22+
private void backtrack(int[] candidates, int target,
23+
List<List<Integer>> result,
24+
List<Integer> path, int index) {
25+
if (target == 0) {
26+
result.add(new ArrayList<>(path));
27+
return;
28+
}
29+
for (int i = index; i < candidates.length; i++) {
30+
int num = candidates[i];
31+
// 已经超量了,则直接返回
32+
if (num > target) {
33+
return;
34+
}
35+
// 同一层,相同的数字只能选一次。否则,就会产生重复的组合。
36+
if (i > index && candidates[i - 1] == candidates[i]) {
37+
continue;
38+
}
39+
path.add(num);
40+
backtrack(candidates, target - num, result, path, i + 1);
41+
path.removeLast();
42+
}
43+
}
44+
45+
// end::answer[]
46+
public static void main(String[] args) {
47+
new _0040_CombinationSumIi_4()
48+
.combinationSum2(new int[]{10, 1, 2, 7, 6, 1, 5}, 8);
49+
}
50+
}

0 commit comments

Comments
 (0)