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()
相关推荐
Python图像识别1 小时前
75_基于深度学习的咖啡叶片病害检测系统(yolo11、yolov8、yolov5+UI界面+Python项目源码+模型+标注好的数据集)
python·深度学习·yolo
闲人编程1 小时前
Python游戏开发入门:Pygame实战
开发语言·python·游戏·pygame·毕设·codecapsule
报错小能手1 小时前
C++笔记(面向对象)静态联编和动态联编
开发语言·c++·算法
WBluuue1 小时前
AtCoder Beginner Contest 430(ABCDEF)
c++·算法
小肖爱笑不爱笑1 小时前
2025/11/5 IO流(字节流、字符流、字节缓冲流、字符缓冲流) 计算机存储规则(ASCII、GBK、Unicode)
java·开发语言·算法
熬了夜的程序员1 小时前
【LeetCode】99. 恢复二叉搜索树
算法·leetcode·职场和发展
Kent_J_Truman1 小时前
LeetCode Hot100 自用
算法·leetcode·职场和发展
还是码字踏实1 小时前
算法题种类与解题思路全面指南:基于LeetCode Hot 100与牛客Top 101
算法·leetcode
雍凉明月夜1 小时前
人工智能学习中深度学习之python基础之 类
python·学习
Geo_V2 小时前
OpenAI 大模型 API 使用示例
python·chatgpt·openai·大模型应用·llm 开发