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()
相关推荐
柳鲲鹏几秒前
关于#pragma pack(push, 8),DeepSeek回答错误
算法
Iridescent11211 分钟前
Iridescent:Day49
python
settingsun122511 分钟前
【AI-算法-01】ResNet (残差网络) & Skip Connections
人工智能·算法
曲幽17 分钟前
从安装到上线:一份 Nginx 实战指南,让你的 Web 应用稳建安全
python·nginx·flask·fastapi·web·gunicorn·uvicorn
vibag27 分钟前
LangSmith监控
人工智能·python·语言模型·langchain·大模型
橘颂TA31 分钟前
【剑斩OFFER】算法的暴力美学——两数之和
数据结构·算法·leetcode·力扣·结构与算法
福楠39 分钟前
C++ STL | vector
开发语言·c++·算法
云里雾里!43 分钟前
力扣 268. 缺失数字 ✅ 【位运算(异或)最优解法】深度解析
算法·leetcode
YJlio1 小时前
PsPing 学习笔记(14.7):一条龙网络体检脚本——连通性、延迟、带宽全都要
开发语言·网络·笔记·python·学习·pdf·php
kaikaile19951 小时前
ISODATA聚类方法在MATLAB中的实现指南
算法·matlab·聚类