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
相关推荐
七夜zippoe几秒前
异步编程实战:构建高性能Python网络应用
开发语言·python·websocket·asyncio·aiohttp
ysn11111几秒前
简单多边形三角剖分---耳切法(含源码)
算法
tianyuanwo1 分钟前
Python虚拟环境深度解析:从virtualenv到virtualenvwrapper
开发语言·python·virtualenv
e疗AI产品之路1 分钟前
一文介绍Philips DXL心电图算法
算法·pan-tompkins·心电分析
越甲八千2 分钟前
ORM 的优势
数据库·python
是有头发的程序猿4 分钟前
Python爬虫防AI检测实战指南:从基础到高级的规避策略
人工智能·爬虫·python
grd44 分钟前
Electron for OpenHarmony 实战:Pagination 分页组件实现
python·学习
CryptoRzz5 分钟前
印度交易所 BSE 与 NSE 实时数据 API 接入指南
java·c语言·python·区块链·php·maven·symfony
YGGP8 分钟前
【Golang】LeetCode 21. 合并两个有序链表
leetcode·链表·golang
小袁顶风作案11 分钟前
leetcode力扣——135.分发糖果
算法·leetcode·职场和发展