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
相关推荐
高洁017 分钟前
智能体:你的私人数字助理
人工智能·python·数据挖掘·virtualenv·知识图谱
海鸥-w7 分钟前
python(fastapi) 实现更新,新增,删除接口
android·python·fastapi
淘矿人8 分钟前
DeepSeek V4对决Claude 4.8:AI模型终极横评
java·开发语言·人工智能·python·sql·php·pygame
showgea17 分钟前
Python httpx封装和使用
python·httpx
玖釉-23 分钟前
编辑距离(Edit Distance)——从字符串相似度到动态规划经典模型
算法·leetcode·动态规划
Asize33 分钟前
重生之我在 Vibe Coding 时代当程序员:第十二课,Prompt 不是咒语,是可以沉淀的业务接口
前端·人工智能·python
c2385636 分钟前
c/c++中的二叉树进阶
c语言·c++·算法
abigale0340 分钟前
字典 与 Python 对象 的总结
python·dict·object
星河漫步Lu43 分钟前
Pycharm中部署Anaconda环境
ide·python·pycharm
吴可可1231 小时前
Win7下C#开发AutoCAD首选版本
算法