|
1 | 1 | [#0377-combination-sum-iv] |
2 | | -= 377. Combination Sum IV |
| 2 | += 377. 组合总和 Ⅳ |
3 | 3 |
|
4 | | -{leetcode}/problems/combination-sum-iv/[LeetCode - Combination Sum IV^] |
| 4 | +https://leetcode.cn/problems/combination-sum-iv/[LeetCode - 377. 组合总和 Ⅳ ^] |
5 | 5 |
|
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` 的元素组合的个数。 |
7 | 7 |
|
8 | | -*Example:* |
| 8 | +题目数据保证答案符合 32 位整数范围。 |
9 | 9 |
|
10 | | -[subs="verbatim,quotes,macros"] |
11 | | ----- |
12 | | -_*nums*_ = [1, 2, 3] |
13 | | -_*target*_ = 4 |
| 10 | +*示例 1:* |
14 | 11 |
|
15 | | -The possible combination ways are: |
| 12 | +.... |
| 13 | +输入:nums = [1,2,3], target = 4 |
| 14 | +输出:7 |
| 15 | +解释: |
| 16 | +所有可能的组合为: |
16 | 17 | (1, 1, 1, 1) |
17 | 18 | (1, 1, 2) |
18 | 19 | (1, 2, 1) |
19 | 20 | (1, 3) |
20 | 21 | (2, 1, 1) |
21 | 22 | (2, 2) |
22 | 23 | (3, 1) |
| 24 | +请注意,顺序不同的序列被视作不同的组合。 |
| 25 | +.... |
23 | 26 |
|
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:* |
32 | 28 |
|
| 29 | +.... |
| 30 | +输入:nums = [9], target = 3 |
| 31 | +输出:0 |
| 32 | +.... |
33 | 33 |
|
34 | | -What if negative numbers are allowed in the given array? |
35 | 34 |
|
| 35 | +*提示:* |
36 | 36 |
|
37 | | -How does it change the problem? |
| 37 | +* `+1 <= nums.length <= 200+` |
| 38 | +* `+1 <= nums[i] <= 1000+` |
| 39 | +* `nums` 中的所有元素 *互不相同* |
| 40 | +* `+1 <= target <= 1000+` |
38 | 41 |
|
| 42 | +**进阶:**如果给定的数组中含有负数会发生什么?问题会产生何种变化?如果允许负数出现,需要向题目中添加哪些限制条件? |
39 | 43 |
|
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. |
46 | 44 |
|
| 45 | +== 思路分析 |
47 | 46 |
|
| 47 | +起初,我以为是深度优先遍历或者回溯。要去验证的时候,发现,这是组合问题,没办法加备忘录。看答案,竟然是 xref:0000-26-dp-2-unbounded-knapsack.adoc[完全背包问题]。 |
48 | 48 |
|
49 | 49 | [[src-0377]] |
| 50 | +[tabs] |
| 51 | +==== |
| 52 | +一刷:: |
| 53 | ++ |
| 54 | +-- |
50 | 55 | [{java_src_attr}] |
51 | 56 | ---- |
52 | | -include::{sourcedir}/_0377_CombinationSumIV.java[tag=answer] |
| 57 | +include::{sourcedir}/_0377_CombinationSumIv.java[tag=answer] |
53 | 58 | ---- |
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. 组合总和 Ⅳ - 本质是爬楼梯:记忆化搜索 / 递推^] |
0 commit comments