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()
相关推荐
汀、人工智能2 小时前
[特殊字符] 第21课:最长有效括号
数据结构·算法·数据库架构·图论·bfs·最长有效括号
花酒锄作田2 小时前
Postgres - Listen/Notify构建轻量级发布订阅系统
python·postgresql
Boop_wu3 小时前
[Java 算法] 字符串
linux·运维·服务器·数据结构·算法·leetcode
Thomas.Sir3 小时前
第二章:LlamaIndex 的基本概念
人工智能·python·ai·llama·llamaindex
故事和你913 小时前
洛谷-算法1-2-排序2
开发语言·数据结构·c++·算法·动态规划·图论
m0_694845573 小时前
Dify部署教程:从AI原型到生产系统的一站式方案
服务器·人工智能·python·数据分析·开源
Fcy6483 小时前
算法基础详解(三)前缀和与差分算法
算法·前缀和·差分
kvo7f2JTy4 小时前
基于机器学习算法的web入侵检测系统设计与实现
前端·算法·机器学习
List<String> error_P4 小时前
蓝桥杯最后几天冲刺:暴力大法(一)
算法·职场和发展·蓝桥杯
李昊哲小课4 小时前
Python办公自动化教程 - 第7章 综合实战案例 - 企业销售管理系统
开发语言·python·数据分析·excel·数据可视化·openpyxl