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()
相关推荐
小嘉丶学长9 小时前
Day08 · 字符串(二)
服务器·开发语言·python·学习·1024程序员节
BD_Marathon9 小时前
【Python】列表推导式
python
2501_948122639 小时前
Qs 跨端三方库仓颉适配全流程
python·react native·django·tornado
荆棘鸟智能9 小时前
扬尘在线监测数据接入与异常清洗:从断点补传到质量审计
人工智能·算法·系统架构
萌动的小火苗9 小时前
MQTT的QoS0、QoS1、QoS2有什么区别?
开发语言·网络·python·php·信息与通信
forest_099 小时前
《大模型原理通俗讲》系列 00|开篇 大模型到底是怎么回事
人工智能·python·prompt·agi
工程师迪菲9 小时前
【基础篇 01】YOLO 到底在做什么?先从一张图片看明白
python
文丑颜不良啊9 小时前
LeetCode926~950题目解析
java·leetcode
happylifetree9 小时前
Python72:核心语法-类型注解-函数的类型注解
python
动词ing9 小时前
【题目练习】旋转图像
算法