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()
相关推荐
music&movie7 分钟前
算法工程师认知水平要求总结
人工智能·算法
心扬21 分钟前
python生成器
开发语言·python
mouseliu27 分钟前
python之二:docker部署项目
前端·python
狂小虎44 分钟前
亲测解决self.transform is not exist
python·深度学习
Python智慧行囊1 小时前
Python 中 Django 中间件:原理、方法与实战应用
python·中间件·架构·django·开发
深科文库1 小时前
构建 MCP 服务器:第 3 部分 — 添加提示
服务器·python·chatgpt·langchain·prompt·aigc·agi
laocui11 小时前
Σ∆ 数字滤波
人工智能·算法
yzx9910131 小时前
Linux 系统中的算法技巧与性能优化
linux·算法·性能优化
蓝婷儿1 小时前
6个月Python学习计划 Day 17 - 继承、多态与魔术方法
开发语言·python·学习