From 1fb4ea4dde61705134d6c4553d6c4d386ae0264d Mon Sep 17 00:00:00 2001 From: xqm32 <458173774@qq.com> Date: Sat, 26 Aug 2023 10:30:36 +0800 Subject: [PATCH] leetcode: finished #39 --- content/leetcode/2023/8.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/content/leetcode/2023/8.md b/content/leetcode/2023/8.md index 962b123a0..99ac1c814 100644 --- a/content/leetcode/2023/8.md +++ b/content/leetcode/2023/8.md @@ -3,6 +3,40 @@ title: "2023.8" draft: false --- +# 2023.8.26 + +```python +# +# @lc app=leetcode.cn id=39 lang=python3 +# +# [39] 组合总和 +# + +# @lc code=start +from typing import List + + +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + res = [] + + def dfs(candidates, target, path): + if target == 0: + res.append(path) + return + elif target < 0: + return + else: + for i in range(len(candidates)): + dfs(candidates[i:], target - candidates[i], path + [candidates[i]]) + + dfs(candidates, target, []) + return res + + +# @lc code=end +``` + # 2023.8.25 ```python