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()
相关推荐
Gary Studio19 小时前
基于PMSM理论研究加实践
算法
HHHHH1010HHHHH19 小时前
JavaScript中利用IIFE创建私有命名空间的经典方案
jvm·数据库·python
木子墨51619 小时前
LeetCode 热题 100 精讲 | 矩阵与图论进阶篇:矩阵置零 · 螺旋矩阵 · 旋转图像 · 搜索二维矩阵 II · 岛屿数量 · 腐烂的橘子
c++·算法·leetcode·矩阵·力扣·图论
干洋芋果果19 小时前
前端学python
开发语言·前端·python
YJlio19 小时前
1 1.2 Windows 账户的分类:管理员 / 标准 / 来宾 + 微软账户 vs 本地账户
人工智能·python·microsoft·ai·chatgpt·openai·agent
Ailan_Anjuxi19 小时前
【附jupyter源码】使用长短期记忆网络(LSTM)实现一个小说写作AI——以训练《西游记》为例
人工智能·算法
stolentime19 小时前
线段树套?——洛谷P7312 [COCI 2018/2019 #2] Sunčanje题解
c++·算法·图论·洛谷
篮子里的玫瑰19 小时前
Python与网络爬虫——列表与元组
开发语言·爬虫·python
wayz1119 小时前
Day 12:支持向量机(SVM)原理与实践
算法·机器学习·支持向量机
knight_9___19 小时前
RAG面试篇8
人工智能·python·面试·agent·rag