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
相关推荐
圣光SG3 分钟前
Java操作题练习(七)
java·开发语言·算法
_Jimmy_1 小时前
Agent 溯源精度提升方案
人工智能·python·langchain
Cx330_FCQ1 小时前
Tmux使用
服务器·git·算法
SamChan902 小时前
在Web应用中集成PDF多语言翻译功能:PDFTranslator API实战指南
前端·python·ai·pdf·yapi·机器翻译
天天进步20152 小时前
Python全栈项目--智能办公自动化系统
开发语言·python
拳里剑气3 小时前
C++算法:多源BFS
c++·算法·宽度优先·多源bfs
颜酱3 小时前
09 | 重构项目结构
人工智能·python·langchain
ysa0510303 小时前
【板子】短序列dp(换成维护更小常数维度的dp)
c++·笔记·算法·板子
shwill1233 小时前
PID 算法(三)--- 增量 PID ↔ 单神经元 PID 等价映射
linux·算法
Sisphusssss4 小时前
香橙派5plus GPIO
linux·python·ubuntu