【力扣】[热题 HOT100] 39.组合总和
发布日期:2021-05-10 06:33:54 浏览次数:16 分类:精选文章

本文共 1971 字,大约阅读时间需要 6 分钟。

������

������������������������������������`candidates`������������������`target`���������`candidates`������������������������������`target`������������

������������������������������������������������������������

������������

���������������������������������������DFS������������������������������������������������������������������������������������������������������������������������������������������������������������

detail:

  • ������������������������dfs���������������nums���������target���������������u������������������sum���������������
  • ���������������sum���������������target������������������������������ans���������������
  • ������������������������������������������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������
  • ������������

    ������������������������������������Python���������
    class Solution:  
    def combinationSum(self, candidates, target):
    # ������������������������������������������������
    candidates.sort()
    # ���������������������������DFS������������������
    self.ans = []
    self.tmp = []
    def dfs(i, sum):
    # ������������������������������������������ans���
    if sum == target:
    self.ans.append(self.tmp.copy())
    return
    # ���������i������������������������
    for i in range(len(nums)-i-1):
    sum += candidates[i]
    self.tmp.append(candidates[i])
    dfs(i, sum)
    self.tmp.pop()
    sum -= candidates[i]
    # ���������DFS������
    dfs(0, 0)
    return self.ans

    ���������������������������������������������������dfs������������������������������������������������������������������������������������������������������������������������������������i������������������������������������������������������������������������������

    上一篇:【力扣】[热题 HOT100] 46.全排列
    下一篇:【力扣】[热题 HOT100] 33.搜索旋转排序数组

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年04月24日 10时38分14秒