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()
相关推荐
fie88892 小时前
matlab打靶法求解两点边值优化问题
开发语言·算法·matlab
skywalk81632 小时前
请结合以下说明,先完成类似python的内置函数。 然后再去完成内置库(标准款) ‌内置函数‌
开发语言·python
酉鬼女又兒2 小时前
零基础入门计算机网络:第一章概述全解(三种交换方式+八大性能指标+体系结构分层+十年考研真题精讲)
网络协议·计算机网络·考研·网络安全·职场和发展·计算机外设·求职招聘
不做无法实现的梦~2 小时前
常见工程分析软件
stm32·嵌入式硬件·算法
郝学胜-神的一滴2 小时前
Python 高级编程 018:深挖 super
开发语言·python·程序人生·软件构建
2401_868534782 小时前
2026年5月系统分析
数据结构·python·tornado
hetao17338372 小时前
2026-05-28~06-02 hetao1733837 的刷题记录
c++·算法
ZhengEnCi2 小时前
O08-单写线程与单读线程冲突分析
算法
专注VB编程开发20年2 小时前
python翻译网页HTML的难题
python·c#·html
仍然.2 小时前
算法题目---优先级队列
算法