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()
相关推荐
谁不学习揍谁!9 小时前
大数据可视化看板:基于电子竞技行业数据大数据可视化分析(详细源码文档等资料)
人工智能·python·信息可视化·stylus
weixin_421585019 小时前
常微分方程
算法
文艺倾年9 小时前
【免训练&测试时扩展】通过任务算术转移思维链能力
人工智能·分布式·算法
curry____3039 小时前
dfs全排列和全组合问题
算法·深度优先
想做功的洛伦兹力110 小时前
2026/2/12日打卡
开发语言·c++·算法
大模型玩家七七10 小时前
技术抉择:微调还是 RAG?——以春节祝福生成为例
android·java·大数据·开发语言·人工智能·算法·安全
你撅嘴真丑10 小时前
蛇形填充数组 与 查找最接近的元素
数据结构·c++·算法
瞎某某Blinder10 小时前
DFT学习记录[3]:material project api使用方法 mp_api调取与pymatgen保存
java·笔记·python·学习
JXL186010 小时前
Dynamic programming
算法