python combination_39.leetcode题目讲解(Python): 组合总和(Combination Sum)

题目如下:

0e6bc5051c4a

题目

解题思路

这道题目的解题思路是,通过 sorted 函数先对候选数字的list进行排序,然后再使用递归的方法来获取各个解。

参考代码, beats 84%

class Solution:

def Solver(self, res, path, candidates, target, idx):

for i in range(idx, len(candidates)):

new_target = target - candidates[i]

if new_target < 0:

return

else:

if new_target == 0:

res.append(path + [candidates[i]])

else:

self.Solver(res, path + [candidates[i]], candidates,

new_target, i)

def combinationSum(self, candidates, target):

"""

:type candidates: List[int]

:type target: int

:rtype: List[List[int]]

"""

path = []

res = []

candidates = sorted(candidates)

self.Solver(res, path, candidates, target, 0)

return res

其它题目:[leetcode题目答案讲解汇总(Python版 持续更新)]

(https://www.jianshu.com/p/60b5241ca28e)

ps:如果您有好的建议,欢迎交流 :-D,

也欢迎访问我的个人博客 苔原带 (www.tundrazone.com)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值