Python实现对于给定的输入,保证和为 target 的不同组合数

题目描述

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。
题目链接:https://leetcode.cn/problems/combination-sum/

测试用例

  • 示例1
输入:candidates = [2,3,6,7], target = 7
输出:[[2,2,3],[7]]
解释:
23 可以形成一组候选,2 + 2 + 3 = 7 。注意 2 可以使用多次。
7 也是一个候选, 7 = 7 。
仅有这两种组合。
  • 示例2
输入: candidates = [2,3,5], target = 8
输出: [[2,2,2,2],[2,3,3],[3,5]]
  • 示例3
输入: candidates = [2], target = 1
输出: []
解决思路

对于这种寻找数字组合的解决思路通常有两种方案:

  1. 对于候选数组里面的元素,采用选和不选来构建树
  2. 使用target作为根节点来构建树

下面对于这两种解决思路我们来使用代码进行实现

  • 采用选和不选来构建树
    在这里插入图片描述
import copy
def combinationSum(candidates, target):
    """采用树的结构选择和不选择的思想来寻找所有组合的可能
    :type candidates: List[int]
    :type target: int
    :rtype: List[List[int]]
    """
    #用来保存组合的结果
    res_com_list = []
    def find_com(target,index,com_list):
        """
        :param target:寻找的目标值
        :param index:索引下标
        :param com_list:符合条件的组合结果
        :return:
        """
        #超出了数组的边界直接返回
        if index == len(candidates):
            return
        #寻找的组合符合条件
        if target == 0:
            res_com_list.append(copy.copy(com_list))
            return
        #直接跳过当前元素
        find_com(target, index + 1,com_list)
        #选择当前元素
        #如果当前元素添加到列表之后target>=0则添加进去
        if target - candidates[index] >= 0:
            #添加符合条件的元素到列表中
            com_list.append(candidates[index])
            #减少target的值,进行下一轮元素的添加
            find_com(target - candidates[index],index,com_list)
            #选择另一条分支
            com_list.remove(com_list[-1])

    find_com(target,0,[])

    return res_com_list
  • 使用target作为根节点来构建树
    在这里插入图片描述
def combinationSum(candidates, target):
    """根据target为根节点来构建树
    :param candidates:
    :param target:
    :return:
    """
    #用来保存最终的组合结果
    res_com_list = []
    def find_com(candidates,index,size,com_list,target):
        """通过回溯法来寻找符合条件的组合
        :param candidates:待候选的数组
        :param index:寻找数组的开始下标位置
        :param size:寻找数组的长度
        :param com_list:用来保存符合条件的组合
        :param target:寻找组合的目标值
        :return:
        """
        if target < 0:
            return
        elif target == 0:
            res_com_list.append(com_list)
            return
        else:
            for i in range(index,size):
                #剪枝处理,对于后面target为负的树就直接跳出寻找
                if target - candidates[i] < 0:
                    break
                find_com(candidates,i,size,com_list+[candidates[i]],target-candidates[i])
    #对候选数组进行排序,为了后面的剪枝
    find_com(candidates,0,len(candidates),[],target)
    return res_com_list
参考
  1. 回溯算法 + 剪枝(回溯经典例题详解)
  2. 组合总和
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修炼之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值