当前位置: 首页 > article >正文

python-leetcode-组合总和

39. 组合总和 - 力扣(LeetCode)

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []

        def backtrack(start, current_combination, current_sum):
            # 如果当前组合的和等于 target,将其添加到结果集中
            if current_sum == target:
                result.append(current_combination.copy())
                return

            # 如果当前组合的和大于 target,直接返回
            if current_sum > target:
                return

            # 遍历候选数字
            for i in range(start, len(candidates)):
                # 将当前数字添加到组合中
                current_combination.append(candidates[i])
                # 递归处理下一个数字(允许重复使用当前数字)
                backtrack(i, current_combination, current_sum + candidates[i])
                # 回溯,移除当前数字
                current_combination.pop()

        # 从第一个数字开始递归
        backtrack(0, [], 0)
        return result


http://www.kler.cn/a/540135.html

相关文章:

  • 增量hdfs数据追平
  • Games202 Lecture11 LTC | Disney principled BRDF | NPR
  • ASP.NET Core JWT Version
  • [LUA ERROR] bad light userdata pointer
  • vscode设置保存时自动缩进和格式化
  • IDEA编写SpringBoot项目时使用Lombok报错“找不到符号”的原因和解决
  • win10 llamafactory模型微调相关① || Ollama运行微调模型
  • 【论文阅读】Comment on the Security of “VOSA“
  • 并查集知识整理、蓝桥杯修改数组
  • 【vue】高德地图AMap.Polyline动态更新画折线,逐步绘制
  • 深度学习-神经机器翻译模型
  • 【1.05版】wordpressAI插件批量生成文章、图片、长尾关键词、文章采集、AI对话等
  • 软件工程 项目管理
  • 使用 mkcert 本地部署启动了 TLS/SSL 加密通讯的 MongoDB 副本集和分片集群
  • mysql 学习12 存储引擎,mysql体系结构
  • 技术栈选择:Vue 还是 React
  • gptme - 终端中的个人 AI 助手
  • 《一》深入了解软件测试工具 JMeter-自我介绍
  • 基于lstm+gru+transformer的电池寿命预测健康状态预测-完整数据代码
  • iOS Swift算法之KDF2
  • 【1】深入解析 SD-WAN:从思科 SD-WAN 视角看现代网络发展
  • 题解:P1005 [NOIP 2007 提高组] 矩阵取数游戏
  • win10向windows server服务器传输文件
  • SQLite3实战教程:从入门到精通
  • 基于SeaTunnel同步mysql数据
  • 第18章 不可变对象设计模式(Java高并发编程详解:多线程与系统设计)