LeetCode(力扣)216. 组合总和 IIIPython

LeetCode216. 组合总和 III

题目链接

https://leetcode.cn/problems/combination-sum-iii/

代码

python 复制代码
class Solution:
    def combinationSum3(self, k: int, n: int) -> List[List[int]]:
        result=[]
        self.backtracking(n, k, 0, 1, [], result)
        return result

    def backtracking(self, targetsum, k, currentsum, startindex, path, result):
        if currentsum > targetsum:
            return 
        
        if len(path) == k:
            if currentsum == targetsum:
                result.append(path[:])
            return
        for i in range(startindex, 9 - (k - len(path)) + 2):
            currentsum += i
            path.append(i)
            self.backtracking(targetsum, k, currentsum, i + 1, path, result)
            currentsum -= i
            path.pop()
相关推荐
always_TT16 小时前
【Python 日志记录:logging 模块入门】
开发语言·python·php
lipku18 小时前
数字人直播开源项目LiveStream
python·开源·数字人·数字人直播
geats人山人海18 小时前
数据结构第六章c语言 树的存储下二叉树的概念和存储
c语言·数据结构·算法
阿童木写作18 小时前
Python实现亚马逊商品图批量智能抠图教程
人工智能·python
axinawang19 小时前
第24课:while循环的应用
python
三亚兴嘉装饰19 小时前
三亚服装店装修
python
小柯南敲键盘19 小时前
速卖通AI图片翻译API集成实战
人工智能·python
漂流瓶jz19 小时前
UVA-12627 奇怪的气球膨胀 题解答案代码 算法竞赛入门经典第二版
算法·图论·递归·aoapc·算法竞赛入门经典·uva·12627
phltxy19 小时前
多智能体系统架构设计:从单体代理到协作网络
python·langchain·系统架构
jarvisuni19 小时前
DeepSeekFlash前端依旧拉垮,而且变慢了很多!
前端·javascript·算法