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()
相关推荐
大可不必了3 小时前
03 函数定义和调用
开发语言·python
I_belong_to_jesus3 小时前
C++关于分支预测的优化1
开发语言·c++·算法
承渊政道4 小时前
【Python编程—从入门到实践】(记分与闯关:用Python+Pygame,打造越战越精彩的太空大战!)
开发语言·python·pycharm·游戏程序·pygame
爱学习的程序媛4 小时前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
imgsq4 小时前
GESP Python 一到四级:备考路线与真题特点
python·青少年编程
eggwyw5 小时前
Anaconda安装卸载的详细流程指南
开发语言·python
2601_967979126 小时前
纯手写:Python安装pip完整教程,搞了几小时,终于成功
python·教程·pip·安装·包管理
Norna6 小时前
缓存文件从没写出来:一个 .json 被当成目录用了
python
aifannihao8 小时前
AI 学习第 14 课:CNN 训 6 轮卡在 0.5352,数据没打乱
pytorch·python·深度学习·cnn·学习路线