LeetCode(力扣)40. 组合总和 IIPython

LeetCode40. 组合总和 II

题目链接

https://leetcode.cn/problems/combination-sum-ii/

代码

python 复制代码
class Solution:
    def backtrackingz(self, candidates, target, result, total, path, startindex):
        if target == total:
            result.append(path[:])
            return 
        for i in range(startindex, len(candidates)):
            if i > startindex and candidates[i] == candidates[i-1]:
                continue
            if total + candidates[i] > target:
                break
            path.append(candidates[i])
            total += candidates[i]
            self.backtrackingz(candidates, target, result, total, path, i + 1)
            total -= candidates[i]
            path.pop()

    def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:  
        result = []
        candidates.sort()
        self.backtrackingz(candidates, target, result, 0, [], 0)
        return result
相关推荐
卫青~护驾!5 分钟前
c++数据结构8——二叉树的性质
数据结构·算法
飞川撸码32 分钟前
【LeetCode 热题100】BFS/DFS 实战:岛屿数量 & 腐烂的橘子(力扣200 / 994 )(Go语言版)
leetcode·深度优先·宽度优先
半桔43 分钟前
【烧脑算法】不定长滑动窗口:从动态调整到精准匹配以灵活特性实现高效破题
数据结构·c++·算法·leetcode·面试·职场和发展·排序算法
哆啦A梦的口袋呀1 小时前
基于Python学习《Head First设计模式》 第一章 策略模式
python·学习·设计模式
不会敲代码的灵长类1 小时前
机器学习算法-逻辑回归
算法·机器学习·逻辑回归
拓端研究室TRL1 小时前
消费者网络购物意向分析:调优逻辑回归LR与决策树模型在电商用户购买预测中的应用及特征重要性优化
人工智能·算法·决策树·机器学习·逻辑回归
进阶的小蜉蝣1 小时前
[leetcode] 二分算法
算法·leetcode·职场和发展
JK0x071 小时前
代码随想录算法训练营 Day61 图论ⅩⅠ Floyd A※ 最短路径算法
算法·图论
初叶 crmeb1 小时前
JAVA单商户易联云小票打印替换模板
java·linux·python
Mi Manchi261 小时前
力扣热题100之对称二叉树
python·算法·leetcode