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()
相关推荐
0 0 04 小时前
CCF-CSP 39-2 水印检查(watermark)【C++】
c++·算法
plus4s5 小时前
2月15日(78,80,81题)
c++·算法·图论
能源系统预测和优化研究5 小时前
【原创改进代码】考虑碳交易与电网交互波动惩罚的共享储能电站优化配置与调度模型
算法·能源
935965 小时前
机考27 翻译21 单词14
c语言·数据结构·算法
瞎某某Blinder6 小时前
DFT学习记录[4] 电子和空穴的有效质量计算全流程
python·学习
回敲代码的猴子6 小时前
2月14日打卡
算法
Liue612312317 小时前
基于YOLO11-C3k2-Faster-CGLU的路面落叶检测与识别系统实现
python
blackicexs7 小时前
第四周第七天
算法
~央千澈~7 小时前
抖音弹幕游戏开发之第8集:pyautogui基础 - 模拟键盘操作·优雅草云桧·卓伊凡
网络·python·websocket·网络协议
期末考复习中,蓝桥杯都没时间学了7 小时前
力扣刷题19
算法·leetcode·职场和发展