mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Update 0039.组合总和.md
补充python代码和注释
This commit is contained in:
@ -265,24 +265,72 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## Python
|
## Python
|
||||||
|
**回溯**
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
|
def __init__(self):
|
||||||
|
self.path = []
|
||||||
|
self.paths = []
|
||||||
|
|
||||||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
|
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
|
||||||
res = []
|
'''
|
||||||
path = []
|
因为本题没有组合数量限制,所以只要元素总和大于target就算结束
|
||||||
def backtrack(candidates,target,sum,startIndex):
|
'''
|
||||||
if sum > target: return
|
self.path.clear()
|
||||||
if sum == target: return res.append(path[:])
|
self.paths.clear()
|
||||||
for i in range(startIndex,len(candidates)):
|
self.backtracking(candidates, target, 0, 0)
|
||||||
if sum + candidates[i] >target: return #如果 sum + candidates[i] > target 就终止遍历
|
return self.paths
|
||||||
sum += candidates[i]
|
|
||||||
path.append(candidates[i])
|
def backtracking(self, candidates: List[int], target: int, sum_: int, start_index: int) -> None:
|
||||||
backtrack(candidates,target,sum,i) #startIndex = i:表示可以重复读取当前的数
|
# Base Case
|
||||||
sum -= candidates[i] #回溯
|
if sum_ == target:
|
||||||
path.pop() #回溯
|
self.paths.append(self.path[:]) # 因为是shallow copy,所以不能直接传入self.path
|
||||||
candidates = sorted(candidates) #需要排序
|
return
|
||||||
backtrack(candidates,target,0,0)
|
if sum_ > target:
|
||||||
return res
|
return
|
||||||
|
|
||||||
|
# 单层递归逻辑
|
||||||
|
for i in range(start_index, len(candidates)):
|
||||||
|
sum_ += candidates[i]
|
||||||
|
self.path.append(candidates[i])
|
||||||
|
self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i-1
|
||||||
|
sum_ -= candidates[i] # 回溯
|
||||||
|
self.path.pop() # 回溯
|
||||||
|
```
|
||||||
|
**剪枝回溯**
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def __init__(self):
|
||||||
|
self.path = []
|
||||||
|
self.paths = []
|
||||||
|
|
||||||
|
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
|
||||||
|
'''
|
||||||
|
因为本题没有组合数量限制,所以只要元素总和大于target就算结束
|
||||||
|
'''
|
||||||
|
self.path.clear()
|
||||||
|
self.paths.clear()
|
||||||
|
|
||||||
|
# 为了剪枝需要提前进行排序
|
||||||
|
candidates.sort()
|
||||||
|
self.backtracking(candidates, target, 0, 0)
|
||||||
|
return self.paths
|
||||||
|
|
||||||
|
def backtracking(self, candidates: List[int], target: int, sum_: int, start_index: int) -> None:
|
||||||
|
# Base Case
|
||||||
|
if sum_ == target:
|
||||||
|
self.paths.append(self.path[:]) # 因为是shallow copy,所以不能直接传入self.path
|
||||||
|
return
|
||||||
|
# 单层递归逻辑
|
||||||
|
# 如果本层 sum + condidates[i] > target,就提前结束遍历,剪枝
|
||||||
|
for i in range(start_index, len(candidates)):
|
||||||
|
if sum_ + candidates[i] > target:
|
||||||
|
return
|
||||||
|
sum_ += candidates[i]
|
||||||
|
self.path.append(candidates[i])
|
||||||
|
self.backtracking(candidates, target, sum_, i) # 因为无限制重复选取,所以不是i-1
|
||||||
|
sum_ -= candidates[i] # 回溯
|
||||||
|
self.path.pop() # 回溯
|
||||||
```
|
```
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
Reference in New Issue
Block a user