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()
相关推荐
2501_9418705612 分钟前
Python在高并发微服务数据同步与分布式事务处理中的实践与优化
leetcode
温轻舟38 分钟前
Python自动办公工具05-Word表中相同内容的单元格自动合并
开发语言·python·word·自动化办公·温轻舟
习习.y2 小时前
python笔记梳理以及一些题目整理
开发语言·笔记·python
撸码猿2 小时前
《Python AI入门》第10章 拥抱AIGC——OpenAI API调用与Prompt工程实战
人工智能·python·aigc
qq_386218992 小时前
Gemini生成的自动搜索和下载论文的python脚本
开发语言·python
vx_vxbs662 小时前
【SSM电影网站】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·python·mysql·小程序·php·idea
2501_941147712 小时前
高并发微服务架构Spring Cloud与Dubbo在互联网优化实践经验分享
leetcode
稚辉君.MCA_P8_Java3 小时前
Gemini永久会员 Java中的四边形不等式优化
java·后端·算法
稚辉君.MCA_P8_Java4 小时前
通义 插入排序(Insertion Sort)
数据结构·后端·算法·架构·排序算法
烤汉堡4 小时前
Python入门到实战:post请求+cookie+代理
爬虫·python