Skip to content

Commit 8502923

Browse files
committed
一刷377
1 parent 3a480e8 commit 8502923

6 files changed

Lines changed: 89 additions & 34 deletions

File tree

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,12 +2665,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
26652665
|Medium
26662666
|
26672667

2668-
//|{counter:codes}
2669-
//|{leetcode_base_url}/combination-sum-iv/[377. Combination Sum IV^]
2670-
//|{source_base_url}/_0377_CombinationSumIV.java[Java]
2671-
//|{doc_base_url}/0377-combination-sum-iv.adoc[题解]
2672-
//|Medium
2673-
//|
2668+
|{counter:codes}
2669+
|{leetcode_base_url}/combination-sum-iv/[377. Combination Sum IV^]
2670+
|{source_base_url}/_0377_CombinationSumIV.java[Java]
2671+
|{doc_base_url}/0377-combination-sum-iv.adoc[题解]
2672+
|Medium
2673+
|
26742674

26752675
|{counter:codes}
26762676
|{leetcode_base_url}/kth-smallest-element-in-a-sorted-matrix/[378. Kth Smallest Element in a Sorted Matrix^]

docs/0000-26-dp-2-unbounded-knapsack.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
== 经典题目
66

77
. xref:0322-coin-change.adoc[322. Coin Change]
8+
. xref:0377-combination-sum-iv.adoc[377. Combination Sum IV]
89
. xref:0518-coin-change-ii.adoc[518. Coin Change 2]
910
. xref:1449-form-largest-integer-with-digits-that-add-up-to-target.adoc[1449. Form Largest Integer With Digits That Add up to Target]
1011
. xref:1547-minimum-cost-to-cut-a-stick.adoc[1547. Minimum Cost to Cut a Stick]

docs/0377-combination-sum-iv.adoc

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,78 @@
11
[#0377-combination-sum-iv]
2-
= 377. Combination Sum IV
2+
= 377. 组合总和 Ⅳ
33

4-
{leetcode}/problems/combination-sum-iv/[LeetCode - Combination Sum IV^]
4+
https://leetcode.cn/problems/combination-sum-iv/[LeetCode - 377. 组合总和 Ⅳ ^]
55

6-
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
6+
给你一个由 *不同* 整数组成的数组 `nums` ,和一个目标整数 `target`。请你从 `nums` 中找出并返回总和为 `target` 的元素组合的个数。
77

8-
*Example:*
8+
题目数据保证答案符合 32 位整数范围。
99

10-
[subs="verbatim,quotes,macros"]
11-
----
12-
_*nums*_ = [1, 2, 3]
13-
_*target*_ = 4
10+
*示例 1:*
1411

15-
The possible combination ways are:
12+
....
13+
输入:nums = [1,2,3], target = 4
14+
输出:7
15+
解释:
16+
所有可能的组合为:
1617
(1, 1, 1, 1)
1718
(1, 1, 2)
1819
(1, 2, 1)
1920
(1, 3)
2021
(2, 1, 1)
2122
(2, 2)
2223
(3, 1)
24+
请注意,顺序不同的序列被视作不同的组合。
25+
....
2326

24-
Note that different sequences are counted as different combinations.
25-
26-
Therefore the output is _*7*_.
27-
----
28-
29-
30-
31-
*Follow up:*
27+
*示例 2:*
3228

29+
....
30+
输入:nums = [9], target = 3
31+
输出:0
32+
....
3333

34-
What if negative numbers are allowed in the given array?
3534

35+
*提示:*
3636

37-
How does it change the problem?
37+
* `+1 <= nums.length <= 200+`
38+
* `+1 <= nums[i] <= 1000+`
39+
* `nums` 中的所有元素 *互不相同*
40+
* `+1 <= target <= 1000+`
3841
42+
**进阶:**如果给定的数组中含有负数会发生什么?问题会产生何种变化?如果允许负数出现,需要向题目中添加哪些限制条件?
3943

40-
What limitation we need to add to the question to allow negative numbers?
41-
42-
*Credits:*
43-
44-
45-
Special thanks to <a href="https://leetcode.com/pbrother/">@pbrother</a> for adding this problem and creating all test cases.
4644

45+
== 思路分析
4746

47+
起初,我以为是深度优先遍历或者回溯。要去验证的时候,发现,这是组合问题,没办法加备忘录。看答案,竟然是 <<0000-26-dp-2-unbounded-knapsack>>。
4848

4949
[[src-0377]]
50+
[tabs]
51+
====
52+
一刷::
53+
+
54+
--
5055
[{java_src_attr}]
5156
----
52-
include::{sourcedir}/_0377_CombinationSumIV.java[tag=answer]
57+
include::{sourcedir}/_0377_CombinationSumIv.java[tag=answer]
5358
----
54-
59+
--
60+
61+
// 二刷::
62+
// +
63+
// --
64+
// [{java_src_attr}]
65+
// ----
66+
// include::{sourcedir}/_0377_CombinationSumIv_2.java[tag=answer]
67+
// ----
68+
// --
69+
====
70+
71+
72+
== 参考资料
73+
74+
. https://oi-wiki.org/dp/knapsack/[背包 DP - OI Wiki^]
75+
. https://leetcode.cn/problems/combination-sum-iv/solutions/124393/xi-wang-yong-yi-chong-gui-lu-gao-ding-bei-bao-wen-/[377. 组合总和 Ⅳ - 希望用一种规律搞定背包问题^]
76+
. https://leetcode.cn/problems/combination-sum-iv/solutions/740651/gong-shui-san-xie-yu-wan-quan-bei-bao-we-x0kn/[377. 组合总和 Ⅳ - 【宫水三叶】本题与「完全背包」问题的主要区别,以及「溢出处理」说明^]
77+
. https://leetcode.cn/problems/combination-sum-iv/solutions/740877/fu-xue-ming-zhu-cong-ji-yi-hua-di-gui-tu-rqwy/[377. 组合总和 Ⅳ - 【负雪明烛】从「记忆化递归」推导「动态规划」]
78+
. https://leetcode.cn/problems/combination-sum-iv/solutions/2706336/ben-zhi-shi-pa-lou-ti-cong-ji-yi-hua-sou-y52j/[377. 组合总和 Ⅳ - 本质是爬楼梯:记忆化搜索 / 递推^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ include::0374-guess-number-higher-or-lower.adoc[leveloffset=+1]
839839

840840
include::0376-wiggle-subsequence.adoc[leveloffset=+1]
841841

842-
// include::0377-combination-sum-iv.adoc[leveloffset=+1]
842+
include::0377-combination-sum-iv.adoc[leveloffset=+1]
843843

844844
include::0378-kth-smallest-element-in-a-sorted-matrix.adoc[leveloffset=+1]
845845

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,11 @@ endif::[]
12441244
|{doc_base_url}/0376-wiggle-subsequence.adoc[题解]
12451245
|❌ 贪心算法。一脸懵逼,看答案才懂。
12461246

1247+
|{counter:codes2503}
1248+
|{leetcode_base_url}/combination-sum-iv/[377. 组合总和 Ⅳ^]
1249+
|{doc_base_url}/0377-combination-sum-iv.adoc[题解]
1250+
|❌ 动态规划 - 完全背包问题。起初以为是回溯,没想到是完全背包问题。
1251+
12471252

12481253
|===
12491254

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0377_CombinationSumIv {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-07-26 22:19:09
8+
*/
9+
public int combinationSum4(int[] nums, int target) {
10+
int[] dp = new int[target + 1];
11+
dp[0] = 1;
12+
for (int i = 1; i <= target; i++) {
13+
for (int j = 0; j < nums.length; j++) {
14+
if (i >= nums[j]) {
15+
dp[i] += dp[i - nums[j]];
16+
}
17+
}
18+
}
19+
return dp[target];
20+
}
21+
// end::answer[]
22+
public static void main(String[] args) {
23+
new _0377_CombinationSumIv().combinationSum4(new int[]{1, 2, 3}, 4);
24+
}
25+
}

0 commit comments

Comments
 (0)