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 分钟前
if 语句对程序性能的影响
算法·架构
用户916357440959 分钟前
LeetCode热题100——15.三数之和
javascript·算法
小虎鲸0014 分钟前
PyTorch的安装与使用
人工智能·pytorch·python·深度学习
加油吧zkf20 分钟前
Python入门:从零开始的完整学习指南
开发语言·前端·python
ting_zh30 分钟前
导数、偏导数与梯度:机器学习数学基础
算法·基础数学
灰灰老师1 小时前
七种排序算法比较与选择[Python ]
java·算法·排序算法
杰瑞学AI1 小时前
我的全栈学习之旅:FastAPI (持续更新!!!)
后端·python·websocket·学习·http·restful·fastapi
用户3721574261351 小时前
Python 高效实现 Excel 与 CSV 互转:用自动化提升效率
python
秃头狂魔1 小时前
DAY1 数组一
算法
CM莫问1 小时前
推荐算法之粗排
深度学习·算法·机器学习·数据挖掘·排序算法·推荐算法·粗排