zoukankan      html  css  js  c++  java
  • Leetcode39 排列组合目标和值可重复

    给定一个无重复元素的数组 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]]:
            candidates.sort()
            res_=[]
            def get_conbine(nums,pre,target):
                if target==0:
                    res_.append(nums[::])
                if target<0:
                    return;
                nums_=nums[::]
                for i in range(pre,len(candidates)):
                    nums.append(candidates[i])
                    get_conbine(nums,i,target-candidates[i])
                    nums.pop()
                return;
            get_conbine([],0,target)
            return res_
  • 相关阅读:
    PHP线程安全和非线程安全有什么区别
    SHOW SLAVE STATUS 详解
    linux 2>&1
    crontab 例行性排程
    clang-format 数组初始化,多行模式
    windows 10 1909 无法启用 .NET Framework 解决
    SysCtlDelay 实现延时
    wpa_supplicant 检测错误密码
    swig python dynamic module does not define init function
    讯飞错误码10116
  • 原文地址:https://www.cnblogs.com/BetterThanEver_Victor/p/12829051.html
Copyright © 2011-2022 走看看