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
相关推荐
天若有情6736 分钟前
逆向玩家狂喜!用C++野生写法一键破解线性加密(不规范但巨好用)
开发语言·c++·算法
风筝在晴天搁浅40 分钟前
剑指Offer 60.n个骰子的点数
算法
ProgramHelpOa44 分钟前
Optiver 2026 OA 全面复盘|26NG / Intern 最新高频题型整理
人工智能·算法·机器学习
feifeigo12344 分钟前
基于无迹变换的电网概率潮流分析 MATLAB 实现
开发语言·算法·matlab
Java成神之路-1 小时前
【算法刷题笔记】全题型导航目录
笔记·算法
爱写代码的倒霉蛋1 小时前
2022年天梯赛L1-8真题解析(哈希+排序)
数据结构·算法
Struggle_97551 小时前
算法知识-倍增算法
算法
计算机安禾1 小时前
【计算机网络】第5篇:网桥学习与生成树算法——环路拓扑中的路径收敛问题
学习·计算机网络·算法
fie88891 小时前
基于遗传算法的机械故障诊断MATLAB程序
算法·机器学习·matlab
nlpming1 小时前
opencode MCP(Model Context Protocol)配置手册
算法