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
相关推荐
岁月宁静4 小时前
五、《从零手撸 Agent》 — 什么是 Agent:从“会聊天”到“会做事”,差的就是一个循环
python·agent
梯度下降者4 小时前
Python - 函数
python·高阶函数·可调用对象·functools·嵌套作用域
forest_095 小时前
《AI 编程 Harness 实战手册》04|审批这道门:点了一百次“允许”,还剩多少作用?
人工智能·算法·prompt·agi
H.莓飛5 小时前
【C++】【STL】stack_OJ题
服务器·c语言·开发语言·数据结构·c++·算法·visual studio
starzy19907 小时前
深入理解Scala隐式参数:上下文流转
开发语言·python·scala
不会武功不懂江湖7 小时前
RISC-V链接器与链接脚本
汇编·算法·risc-v
wuyk5557 小时前
Spider-第16讲-线程池与竞态问题
大数据·爬虫·python·elasticsearch·搜索引擎
Zwawa8 小时前
卡尔曼到底在算什么:从一维到它的变式以及工程版卡尔曼滤波算法C语言实现
c语言·算法·嵌入式
电科_银尘8 小时前
【大模型算法与工程 14】Tokenizer与Embedding:文本如何变成模型输入
算法·embedding