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
相关推荐
CClaris7 分钟前
大模型量化从0到1(九):用 llama.cpp 把模型转成 GGUF 并跑本地推理
人工智能·pytorch·python·深度学习·llama
学编程的小虎16 分钟前
SenseVoice微调
人工智能·python·自然语言处理
c2385631 分钟前
Bug 猎手入门指南
c++·算法·bug
诸葛说抛光40 分钟前
国内大型汽车改装展览会定展 佛山改装 佛山汽车赛事
python·汽车
chouchuang1 小时前
day-030-综合练习-笔记管理器
开发语言·笔记·python
乖巧的妹子1 小时前
Python基础核心知识点详解:内置函数、运算符、字符串方法、数据结构与类型转换
python
Reart1 小时前
Leetcode 213.打家劫舍2(内含闲谈,打劫真是技术活,好题,716)
后端·算法
幸福清风2 小时前
Python 完美处理Excel合并单元格:拆分填充+自动合并
python·excel·合并单元格·拆分单元格
Reart2 小时前
Leetcode 198.打家劫舍(716)
后端·算法
Jerry2 小时前
LeetCode 110. 平衡二叉树
算法