Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c02e94e

Browse files
add LeetCode 216. 组合总和 III
1 parent c4bf821 commit c02e94e

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
找出所有相加之和为 `n``k` 个数的组合。组合中只允许含有 `1 - 9` 的正整数,并且每种组合中不存在重复的数字。
6+
7+
说明:
8+
9+
所有数字都是正整数。
10+
解集不能包含重复的组合。
11+
示例 1:
12+
13+
```javascript
14+
输入: k = 3, n = 7
15+
输出: [[1,2,4]]
16+
```
17+
18+
示例 2:
19+
20+
```javascript
21+
输入: k = 3, n = 9
22+
输出: [[1,2,6], [1,3,5], [2,3,4]]
23+
```
24+
25+
来源:力扣(LeetCode)
26+
链接:https://leetcode-cn.com/problems/combination-sum-iii
27+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
28+
29+
30+
31+
32+
## 解题思路
33+
首先,还是搬运一下大佬的图解,然后我再来解释一番。
34+
![](https://img-blog.csdnimg.cn/20200918154008515.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjQyOTcxOA==,size_16,color_FFFFFF,t_70#pic_center)
35+
<a href="https://leetcode-cn.com/problems/combination-sum-iii/solution/shou-hua-tu-jie-216-zu-he-zong-he-iii-by-xiao_ben_/">参考xiao_ben_zhu大佬图解</a>
36+
37+
本题需要一层一层来,第一层我们可以有 `i`(1-9)个选择,而第二层的每一个值只有 `i+1`个选择了,因为不能重复。比如你第一次拿了 `2`,在下一次,你只能从 `3`开始拿了,如果还是 `1`的话就会有重复的组合了。这样我们也不用维护 `vis`数组来去重,因为每一层取的值是不一样的。
38+
39+
```javascript
40+
var combinationSum3 = function (k, n) {
41+
let res = [];
42+
let dfs = (t, start, sum) => {
43+
if (t.length === k && sum === n) {
44+
res.push(t);
45+
}
46+
for (let i = start; i < 10; i++) {
47+
t.push(i);
48+
dfs(t.slice(), i + 1, sum + i);
49+
t.pop();
50+
}
51+
}
52+
dfs([], 1, 0);
53+
return res;
54+
};
55+
```
56+
57+
58+
59+
## 最后
60+
文章产出不易,还望各位小伙伴们支持一波!
61+
62+
往期精选:
63+
64+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
65+
66+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
67+
68+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
69+
70+
```javascript
71+
学如逆水行舟,不进则退
72+
```
73+
74+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /