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
相关推荐
qq74223498411 分钟前
Python操作数据库之pyodbc
开发语言·数据库·python
gihigo199838 分钟前
MATLAB使用遗传算法解决车间资源分配动态调度问题
算法·matlab
墨染点香44 分钟前
LeetCode 刷题【138. 随机链表的复制】
算法·leetcode·链表
2401_841495641 小时前
【自然语言处理】轻量版生成式语言模型GPT
人工智能·python·gpt·深度学习·语言模型·自然语言处理·transformer
却道天凉_好个秋1 小时前
目标检测算法与原理(一):迁移学习
算法·目标检测·迁移学习
云和数据.ChenGuang1 小时前
tensorflow生成随机数和张量
人工智能·python·tensorflow
兮山与2 小时前
算法24.0
算法
测试老哥2 小时前
python+requests+excel 接口测试
自动化测试·软件测试·python·测试工具·测试用例·excel·接口测试