递归和回溯_leetcode39

class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
self.res = []



self.generateCombinatio(candidates,target,0,[])

print self.res

return self.res


# start 指示序列的总终止条件
def generateCombinatio(self,candidates,target,start ,ans):

if sum(ans) == target:
self.res.append(ans[0:])
return

if sum(ans) > target:
return

#[i,n] 防止 重复搜索
for i in range(start,len(candidates)):

ans.append(candidates[i])
self.generateCombinatio(candidates,target,i,ans)
ans.pop()





s = Solution()
can = [2,3,6,7]
tar = 7

s.combinationSum(can,tar)

猜你喜欢

转载自www.cnblogs.com/lux-ace/p/10556930.html