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()
相关推荐
麦麦大数据10 分钟前
F047 vue3+flask微博舆情推荐可视化问答系统
python·flask·知识图谱·neo4j·推荐算法·舆情分析·舆情监测
MediaTea13 分钟前
Python 第三方库:Flask(轻量级 Web 框架)
开发语言·前端·后端·python·flask
Kuo-Teng16 分钟前
LeetCode 198: House Robber
java·算法·leetcode·职场和发展·动态规划
2501_9411114017 分钟前
C++中的状态模式实战
开发语言·c++·算法
SelectDB27 分钟前
十亿 JSON 秒级响应:Apache Doris vs ClickHouse,Elasticsearch,PostgreSQL
算法
java干货33 分钟前
Spring Boot 为什么“抛弃”了 spring.factories?
spring boot·python·spring
2501_941111821 小时前
使用Python进行网络设备自动配置
jvm·数据库·python
源码之家1 小时前
基于python租房大数据分析系统 房屋数据分析推荐 scrapy爬虫+可视化大屏 贝壳租房网 计算机毕业设计 推荐系统(源码+文档)✅
大数据·爬虫·python·scrapy·数据分析·推荐算法·租房
橘颂TA1 小时前
【剑斩OFFER】算法的暴力美学——除自身以外数组的乘积
算法·leetcode·职场和发展·结构与算法