添加 0078.子集 python3版本

添加 0078.子集 python3版本
This commit is contained in:
jojoo15 2021-05-27 18:01:48 +02:00 committed by GitHub
parent fa25fab461
commit ce1a80b016
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 1 deletions

View File

@ -205,7 +205,20 @@ class Solution {
```
Python
```python3
class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = []
path = []
def backtrack(nums,startIndex):
res.append(path[:]) #收集子集,要放在终止添加的上面,否则会漏掉自己
for i in range(startIndex,len(nums)): #当startIndex已经大于数组的长度了就终止了for循环本来也结束了所以不需要终止条件
path.append(nums[i])
backtrack(nums,i+1) #递归
path.pop() #回溯
backtrack(nums,0)
return res
```
Go
```Go