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 e5ef292

Browse files
add LeetCode 78. 子集
1 parent 989365f commit e5ef292

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

‎递归与回溯/LeetCode 78. 子集.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4uanNkZWxpdnIubmV0L2doL2Nob2NvbGF0ZTE5OTkvY2RuL2ltZy8yMDIwMDgyODE0NTUyMS5qcGc?x-oss-process=image/format,png)
2+
>仰望星空的人,不应该被嘲笑
3+
4+
## 题目描述
5+
6+
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
7+
8+
说明:解集不能包含重复的子集。
9+
10+
示例:
11+
12+
```clike
13+
输入: nums = [1,2,3]
14+
输出:
15+
[
16+
[3],
17+
[1],
18+
[2],
19+
[1,2,3],
20+
[1,3],
21+
[2,3],
22+
[1,2],
23+
[]
24+
]
25+
```
26+
27+
来源:力扣(LeetCode)
28+
链接:https://leetcode-cn.com/problems/subsets
29+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
30+
31+
32+
33+
## 解题思路
34+
一道组合相关的题目,采用回溯来做即可,题目说明不包含重复元素,于是我们也无需排序然后判断相邻元素是否相等来去重了。
35+
36+
```javascript
37+
var subsets = function(nums) {
38+
let res = [];
39+
let dfs = (t,start) => {
40+
res.push(t);
41+
for(let i=start;i<nums.length;i++){
42+
t.push(nums[i]);
43+
dfs(t.slice(),i+1);
44+
t.pop();
45+
}
46+
}
47+
dfs([],0);
48+
return res;
49+
};
50+
```
51+
52+
53+
## 最后
54+
文章产出不易,还望各位小伙伴们支持一波!
55+
56+
往期精选:
57+
58+
<a href="https://github.com/Chocolate1999/Front-end-learning-to-organize-notes">小狮子前端の笔记仓库</a>
59+
60+
<a href="https://yangchaoyi.vip/">访问超逸の博客</a>,方便小伙伴阅读玩耍~
61+
62+
![](https://img-blog.csdnimg.cn/2020090211491121.png#pic_center)
63+
64+
```javascript
65+
学如逆水行舟,不进则退
66+
```
67+
68+

0 commit comments

Comments
(0)

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