ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

【力扣100】39.组合总和

2026/9/24 0:55:24 拓冰建站 浏览量
【力扣100】39.组合总和

添加链接描述

class Solution:def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:def backtrack(path,target,res,index):if target==0:res.append(path[:])returnif target<0:return for i in range(index,len(candidates)):if target>=candidates[i]:path.append(candidates[i])backtrack(path,target-candidates[i],res,i)path.pop()res=[]backtrack([],target,res,0)return res

思路:

  1. 递归与回溯,一眼树形结构,找到想要的节点就可以
    在这里插入图片描述