40. 组合总和 II

'''
Author: 365JHWZGo
Description: 40. 组合总和 II
Date: 2021-10-15 09:27:51
FilePath: \test\demo5.py
LastEditTime: 2021-10-15 11:49:35
LastEditors: 365JHWZGo
'''

class Solution(object):
    def combinationSum2(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        #处理特殊情况
        candidates.sort()
        if candidates[0]>target:
            return []
        if len(set(candidates))==1:
            if sum(candidates)==target:
                return [candidates]
            if sum(candidates)<target:            
                return []
            if sum(candidates)>target:
                if target%candidates[0]==0:
                    return [[candidates[0] for i in range(target//candidates[0])]]
                else:
                    return []
        
        candidates = list(filter(lambda x: x <= target, candidates))
        res = []

        def backtracking(c, t, res, pres, start):
            if t == 0:
                pres = pres + []
                if pres not in res:                  
                    res.extend([pres])
                return
            for i in range(start, len(c)):
                if t < c[i]:
                    return                
                pres.append(c[i])                
                backtracking(c, t - c[i], res, pres, i + 1)
                pres.pop()
            return res

        backtracking(candidates, target, res, [], 0)
        return res

在这里插入图片描述

class Solution(object):
    def combinationSum2(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        candidates.sort()
        if candidates[0]>target:
            return []
        if len(set(candidates))==1:
            if sum(candidates)==target:
                return [candidates]
            if sum(candidates)<target:            
                return []
            if sum(candidates)>target:
                if target%candidates[0]==0:
                    return [[candidates[0] for i in range(target//candidates[0])]]
                else:
                    return []
        
        candidates = list(filter(lambda x: x <= target, candidates))
        res = []
        uses = [0 for i in range(len(candidates))]

        def backtracking(c, t, res, pres, start,uses):
            if t == 0:
                pres = pres + []                                
                res.extend([pres])
                return
            
            for i in range(start, len(c)):
            #优化部分,当同时出现相同的数字时,只进行一次递归查找
                if i>0 and uses[i-1] == 0 and c[i] ==c[i-1]:
                    continue
                if t < c[i]:
                    return                
                pres.append(c[i])    
                uses[i]=1           
                backtracking(c, t - c[i], res, pres, i + 1,uses)
                uses[i]= 0
                pres.pop()
            return res

        backtracking(candidates, target, res, [], 0,uses)
        return res

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

365JHWZGo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值