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
相关推荐
咕白m6259 小时前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python
浮生望9 小时前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰9 小时前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法
地平线开发者20 小时前
J6B vio scenario sample
算法
SelectDB1 天前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
BothSavage1 天前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn1 天前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽1 天前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
荣码1 天前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python