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
相关推荐
Hi_kenyon10 小时前
FastAPI+VUE3创建一个项目的步骤模板(一)
python·fastapi
q_302381955610 小时前
破局路侧感知困境:毫米波雷达+相机融合算法如何重塑智能交通
数码相机·算法
Robert--cao10 小时前
人机交互(如 VR 手柄追踪、光标移动、手势识别)的滤波算法
人工智能·算法·人机交互·vr·滤波器
云青山水林10 小时前
算法竞赛从入门到跳楼(ACM-XCPC、蓝桥杯软件赛等)
c++·算法·蓝桥杯
LYFlied10 小时前
【每日算法】LeetCode138. 随机链表的复制
数据结构·算法·leetcode·链表
zore_c10 小时前
【C语言手撕算法】LeetCode-142. 环形链表 II(C语言)
c语言·数据结构·算法·leetcode·链表·推荐算法
yaoh.wang18 小时前
力扣(LeetCode) 13: 罗马数字转整数 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
T1ssy18 小时前
布隆过滤器:用概率换空间的奇妙数据结构
算法·哈希算法
hetao173383719 小时前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
小鸡吃米…19 小时前
Python PyQt6教程七-控件
数据库·python