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()
相关推荐
CoderYanger15 小时前
A.每日一题——1523. 在区间范围内统计奇数数目
java·数据结构·算法·leetcode·职场和发展
surtr115 小时前
Round 1019(div2) CD
数据结构·c++·算法·贪心算法·stl
Rookie_JE15 小时前
python docxtpl库学习
python
我不是小upper15 小时前
CNN+BiLSTM !!最强序列建模组合!!!
人工智能·python·深度学习·神经网络·cnn
Hcoco_me15 小时前
大模型面试题14:K-means聚类算法全解析(通用场景+深度拓展)
算法·kmeans·聚类
锐学AI15 小时前
从零开始学MCP(四)- 认识MCP clients
人工智能·python
Jay200211115 小时前
【机器学习】30 基于内容的过滤算法
人工智能·算法·机器学习
冰西瓜60015 小时前
分治(二)算法设计与分析 国科大
数据结构·算法
小小晓.16 小时前
Pinely Round 2 (Div. 1 + Div. 2)
c++·算法