题目:39. 组合总和
我的感悟:
- 还是没太理解这个index和i 的区别
- 感觉要继续听继续做
- 剪枝要进行排序,这题,我先理解到不剪枝的版本就行
代码示例:
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
total = 0
self.backtrcaking(candidates,target,0,total,[],res)
return res
def backtrcaking(self,candidates,target,start_index,total,path,res):
# 确定跳出条件
if total > target:
return
if total == target:
res.append(path[:])
# 核心代码
for i in range(start_index,len(candidates)):
path.append(candidates[i])
total += candidates[i]
self.backtrcaking(candidates,target,i,total,path,res)
total -= candidates[i]
path.pop()
通过截图:
扩展写法:
资料:
题目链接/文章讲解:https://programmercarl.com/0039.%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C.html
视频讲解:https://www.bilibili.com/video/BV1KT4y1M7HJ
标签:39,target,res,day27,随想录,candidates,path,total,self From: https://www.cnblogs.com/liqi175/p/17995329