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
相关推荐
水蓝烟雨6 分钟前
[HOT 100] 2187. 完成旅途的最少时间
算法·hot 100
豌豆花下猫11 分钟前
Python 潮流周刊#90:uv 一周岁了,优缺点分析(摘要)
后端·python·ai
橘猫云计算机设计30 分钟前
基于SSM的《计算机网络》题库管理系统(源码+lw+部署文档+讲解),源码可白嫖!
java·数据库·spring boot·后端·python·计算机网络·毕设
小伍_Five32 分钟前
从0开始:OpenCV入门教程【图像处理基础】
图像处理·python·opencv
m0_748245341 小时前
python——Django 框架
开发语言·python·django
01_1 小时前
力扣hot100——LRU缓存(面试高频考题)
leetcode·缓存·面试·lru
菜鸟一枚在这1 小时前
深度解析建造者模式:复杂对象构建的优雅之道
java·开发语言·算法
java1234_小锋1 小时前
一周学会Flask3 Python Web开发-客户端状态信息Cookie以及加密
前端·python·flask·flask3
gyeolhada1 小时前
2025蓝桥杯JAVA编程题练习Day5
java·数据结构·算法·蓝桥杯
阿巴~阿巴~1 小时前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先