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
相关推荐
Smilecoc12 小时前
方向导数与梯度
算法
瑞码空间12 小时前
01背包:动态规划的Hello World
c++·算法·0/1背包问题
程序员雷欧12 小时前
LongAdder
开发语言·python
月光船幽幽12 小时前
分层阈值规避归藏协议过度重置
人工智能·python
Wang's Blog12 小时前
AI Agent白手起家63: LangGraph 人机交互——让人类介入 AI 工作流
人工智能·算法·人机交互
SMF191912 小时前
【Linux】完美解决缩略图工具gm调用java.io.FileNotFoundException: gm问题
java·开发语言·python
㳺三才人子12 小时前
初探 Data Analysis - Matplotlib
python·plotly·pandas·matplotlib
Python私教13 小时前
0、null、未采集:AI Agent 反馈系统最容易踩的语义坑
人工智能·python
北斗落凡尘13 小时前
LangGraph 入门实战(5)
python·langchain
乐观勇敢坚强的老彭13 小时前
C++ STL 常用容器的速查表
java·c++·算法