mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0039.组合总和.md
补充python代码和注释
This commit is contained in:
@ -264,25 +264,73 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
## Python
|
||||
**回溯**
|
||||
```python3
|
||||
class Solution:
|
||||
def __init__(self):
|
||||
self.path = []
|
||||
self.paths = []
|
||||
|
||||
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
|
||||
'''
|
||||
因为本题没有组合数量限制,所以只要元素总和大于target就算结束
|
||||
'''
|
||||
self.path.clear()
|
||||
self.paths.clear()
|
||||
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
|
||||
if sum_ > target:
|
||||
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
|
||||
|
Reference in New Issue
Block a user