• leetcode 39. 组合总和(python)


    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

    candidates 中的数字可以无限制重复被选取。

    说明:

    所有数字(包括 target)都是正整数。
    解集不能包含重复的组合。 
    示例 1:

    输入: candidates = [2,3,6,7], target = 7,
    所求解集为:
    [
    [7],
    [2,2,3]
    ]
    示例 2:

    输入: candidates = [2,3,5], target = 8,
    所求解集为:
    [
      [2,2,2,2],
      [2,3,3],
      [3,5]
    ]

    class Solution:
        def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
            self.res = []
            if len(candidates) <= 0:
                return res
            candidates.sort()
            self.dfs(candidates,[],target,0)  # 递归回溯
            return self.res
                
        def dfs(self,candidates,sublist,target,last):  # last表示当前sublist的最后一个元素
            if target == 0:
                self.res.append(sublist) 
            if target < candidates[0]:  # 剪枝操作,目标值小于拥有的最小元素
                return 
            for num in candidates:  # 数字可重复使用,则每次从头遍历
                if num > target:  # 剪枝操作,当当前数值大于目标值,则后续无需遍历
                    return 
                if num < last: # 剪枝操作:若当前数值小于当前sublist的最后一个元素,则继续遍历,防止出现重复解决方案,如[2,2,3],[3,2,2]
                    continue
                self.dfs(candidates,sublist+[num],target-num,num)

    参考: https://blog.csdn.net/weixin_40546602/article/details/88357837 

    递归函数 dfs(candidates,sublist,target,last),其中sublist记录当前满足条件的子数组,last为当前子数组的最后一个元素。

    剪枝操作1:目标值小于元素数组的最小元素,则无需继续遍历

    剪枝操作2:当前元素大于目标值,则后续元素一定大于目标值(数组已排序),不会满足条件,无需继续遍历

    剪枝操作3:若当前数值小于当前sublist的最后一个元素,则继续遍历,防止出现重复解决方案,如[2,2,3],[3,2,2]

  • 相关阅读:
    Oracle 树操作(select…start with…connect by…prior)
    ORACLE序列的使用总结
    TNS-00512: Address already in use-TNS-12542: TNS:address already in use
    zabbix server is not running: the information displayed may not be current
    fuser:用文件或者套接口表示进程
    E514:write error(file system full?)
    Oracle中drop user和drop user cascade的区别
    Oracle字符集查看
    k-means聚类算法C++实现
    名校公开课网站汇总
  • 原文地址:https://www.cnblogs.com/xiaotongtt/p/11324330.html
Copyright © 2020-2023  润新知