打卡题目:leetcode第39题,组合总和
    相似题目:40
    image.png


    【代码】

    1. class Solution:
    2. def __init__(self):
    3. self.res = []
    4. self.candidates = []
    5. def get_candidates(self, candidates):
    6. candidates.sort()
    7. self.candidates = candidates
    8. def get_res(self, sums, target, now, loc):
    9. if sums + self.candidates[0] > target:
    10. return False
    11. for i in range(len(self.candidates)):
    12. if i < loc:
    13. continue
    14. num = self.candidates[i]
    15. if num + sums == target:
    16. self.res.append(now+[num])
    17. elif num + sums > target:
    18. return False
    19. else:
    20. self.get_res(sums+num, target, now+[num], i)
    21. def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
    22. self.get_candidates(candidates)
    23. self.get_res(0, target, [], 0)
    24. return self.res