import copy
class Solution:
"""
@param candidates: Given the candidate numbers
@param target: Given the target number
@return: All the combinations that sum to target
"""
def combinationSum2(self, candidates, target):
# write your code here
self.ans = []
res = []
self.my_combinationSum2(candidates, target, res, 0)
return self.ans
def my_combinationSum2(self, candidates, target, res, index):
if sum(res) == target and sorted(res) not in self.ans:
self.ans.append(copy.copy(sorted(res)))
return
if sum(res) > target:
return
for index in range(index, len(candidates)):
res.append(candidates[index])
self.my_combinationSum2(candidates, target, res, index+1)
res.pop()
LintCode: 数字组合 II
最新推荐文章于 2020-01-10 20:02:40 发布