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()
相关推荐
仙女修炼史2 小时前
gradcam在yolo中的应用
人工智能·python·yolo
带多刺的玫瑰2 小时前
Leecode#15刷题之三数之和
算法·leetcode·职场和发展
圣保罗的大教堂2 小时前
leetcode 877. 石子游戏 中等
leetcode
哭泣方源炼蛊2 小时前
并查集进阶 P1(带权并查集,并查集分类)
数据结构·c++·算法·二进制·带权并查集
李可以量化3 小时前
Redis Client 从了解到精通(六):redis-py 服务控制与状态监控辅助函数详解
python
牧羊人.3333 小时前
动手学深度学习 02 | 手写数字识别
图像处理·人工智能·深度学习·算法
李可以量化3 小时前
Redis Client 从了解到精通(六)下:redis-py 时间、库管理与持久化辅助函数详解
python
晓窗科技3 小时前
AI基座哪家好
大数据·人工智能·python
武帝为此3 小时前
【InnoDB存储引擎介绍】
数据库·算法
麻雀飞吧3 小时前
学量化:看到“近期工具推荐”时,先问工具要解决什么问题
人工智能·python