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
相关推荐
Alfred king1 小时前
面试150 生命游戏
leetcode·游戏·面试·数组
水木兰亭1 小时前
数据结构之——树及树的存储
数据结构·c++·学习·算法
思则变1 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
June bug2 小时前
【软考中级·软件评测师】下午题·面向对象测试之架构考点全析:分层、分布式、微内核与事件驱动
经验分享·分布式·职场和发展·架构·学习方法·测试·软考
漫谈网络2 小时前
WebSocket 在前后端的完整使用流程
javascript·python·websocket
Jess072 小时前
插入排序的简单介绍
数据结构·算法·排序算法
老一岁2 小时前
选择排序算法详解
数据结构·算法·排序算法
xindafu2 小时前
代码随想录算法训练营第四十二天|动态规划part9
算法·动态规划
xindafu2 小时前
代码随想录算法训练营第四十五天|动态规划part12
算法·动态规划
ysa0510303 小时前
Dijkstra 算法#图论
数据结构·算法·图论