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()
相关推荐
程序员龙语几秒前
CSS 盒模型与文档流
人工智能·python·tensorflow
ZAz_1 分钟前
DAY 46 Tensorborad使用介绍
python
月明长歌5 分钟前
【码道初阶】Leetcode136:只出现一次的数字:异或一把梭 vs HashMap 计数(两种解法完整复盘)
java·数据结构·算法·leetcode·哈希算法
龘龍龙6 分钟前
Python基础学习(七)
开发语言·python·学习
Swift社区10 分钟前
LeetCode 456 - 132 模式
java·算法·leetcode
LYFlied11 分钟前
【每日算法】LeetCode 152. 乘积最大子数组(动态规划)
前端·算法·leetcode·动态规划
MediaTea11 分钟前
Python 库手册:wave WAV 音频读写工具
开发语言·python·音视频
写代码的【黑咖啡】12 分钟前
python的小型实践项目
开发语言·python
圣保罗的大教堂15 分钟前
leetcode 3075. 幸福值最大化的选择方案 中等
leetcode
zyxczyf12319 分钟前
软件工程test
python