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()
相关推荐
ScilogyHunter13 小时前
SCons:Python驱动的智能构建系统
python·构建系统·scons
鱼跃鹰飞13 小时前
Leetcode尊享面试100题:1060. 有序数组中的缺失元素
算法·leetcode·面试
luoluoal13 小时前
基于python的基于深度学习的车俩特征分析系(源码+文档)
python·mysql·django·毕业设计·源码
啊我不会诶13 小时前
AtCoder Beginner Contest 438 vp补题
算法
computersciencer13 小时前
用最小二乘法求解一元一次方程模型的参数
算法·机器学习·最小二乘法
轻竹办公PPT14 小时前
2026 年 AI 办公趋势:AI 生成 PPT 工具谁在领先
人工智能·python
mit6.82414 小时前
扫描线|离散化|seg+二分|卡常
算法
不穿格子的程序员14 小时前
从零开始写算法——二叉树篇6:二叉树的右视图 + 二叉树展开为链表
java·算法·链表
大志若愚YYZ14 小时前
ROS2学习 C++中的this指针
c++·学习·算法