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()
相关推荐
张道宁13 小时前
从零开始训练YOLO手机检测模型:完整实战教程
python·yolo
快乐的哈士奇13 小时前
对话框打字机效果:Vur + Java/Python 实现
java·开发语言·python
周末也要写八哥13 小时前
算法实例分析:使数组相等的最小开销
算法
malog_13 小时前
PyTorch图像数据加载实战指南
图像处理·人工智能·pytorch·python
博.闻广见13 小时前
AI_Python基础-4.标准库与IO
开发语言·python
程序猿编码13 小时前
大模型的“文字障眼法“:FlipAttack 文本反转越狱技术全解析
linux·python·ai·大模型
吃好睡好便好13 小时前
在Matlab中绘制质点运动轨迹图
开发语言·学习·算法·matlab·信息可视化
爱炼丹的James13 小时前
第三章 搜索和图论
数据结构·算法·图论
菜菜笔记13 小时前
【无标题】
算法
晚烛13 小时前
CANN 数据流与内存优化:L1/L2 缓存机制与计算重叠深度解析
人工智能·python·缓存