Skip to content

Commit ce1a80b

Browse files
authored
添加 0078.子集 python3版本
添加 0078.子集 python3版本
1 parent fa25fab commit ce1a80b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

problems/0078.子集.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,20 @@ class Solution {
205205
```
206206

207207
Python:
208-
208+
```python3
209+
class Solution:
210+
def subsets(self, nums: List[int]) -> List[List[int]]:
211+
res = []
212+
path = []
213+
def backtrack(nums,startIndex):
214+
res.append(path[:]) #收集子集,要放在终止添加的上面,否则会漏掉自己
215+
for i in range(startIndex,len(nums)): #当startIndex已经大于数组的长度了,就终止了,for循环本来也结束了,所以不需要终止条件
216+
path.append(nums[i])
217+
backtrack(nums,i+1) #递归
218+
path.pop() #回溯
219+
backtrack(nums,0)
220+
return res
221+
```
209222

210223
Go:
211224
```Go

0 commit comments

Comments
 (0)