Skip to content

Commit

Permalink
添加 0039.组合总数 python3版本
Browse files Browse the repository at this point in the history
添加 0039.组合总数 python3版本
  • Loading branch information
jojoo15 authored May 25, 2021
1 parent e9f8cda commit 3402771
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions problems/0039.组合总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,25 @@ class Solution {
```

Python:


```python3
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
path = []
def backtrack(candidates,target,sum,startIndex):
if sum > target: return
if sum == target: return res.append(path[:])
for i in range(startIndex,len(candidates)):
if sum + candidates[i] >target: return #如果 sum + candidates[i] > target 就终止遍历
sum += candidates[i]
path.append(candidates[i])
backtrack(candidates,target,sum,i) #startIndex = i:表示可以重复读取当前的数
sum -= candidates[i] #回溯
path.pop() #回溯
candidates = sorted(candidates) #需要排序
backtrack(candidates,target,0,0)
return res
```
Go:


Expand Down

0 comments on commit 3402771

Please sign in to comment.