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()
相关推荐
黛色正浓10 小时前
leetCode-热题100-普通数组合集(JavaScript)
java·数据结构·算法
元亓亓亓11 小时前
LeetCode热题100--5. 最长回文子串--中等
linux·算法·leetcode
sunywz11 小时前
【JVM】(2)java类加载机制
java·jvm·python
千金裘换酒11 小时前
LeetCode 环形链表+升级版环形链表
算法·leetcode·链表
小鸡吃米…11 小时前
机器学习中的随机森林算法
算法·随机森林·机器学习
Silence_Jy11 小时前
GPU架构
python
kwg12611 小时前
本地搭建 OPC UA MCP 服务
python·agent·mcp
belldeep11 小时前
python:mnist 数据集下载,parse
python·numpy·mnist
霁月中11 小时前
[Codeforces Round 1065 (Div. 3)](A-D,F)
算法
世洋Blog11 小时前
算法导论-分治法和合并(Merge)排序
算法