From 63be339113eb0d8083ace45f26808a8989f51de4 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Mon, 24 May 2021 14:47:48 +0200 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200216.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E6=80=BB=E6=95=B0=20python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0216.组合总数 python3版本 --- problems/0216.组合总和III.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md index 67468eb8..0aef7aec 100644 --- a/problems/0216.组合总和III.md +++ b/problems/0216.组合总和III.md @@ -262,7 +262,25 @@ class Solution { ``` Python: - +```python3 +class Solution: + def combinationSum3(self, k: int, n: int) -> List[List[int]]: + res = [] #存放结果集 + path = [] #符合条件的结果 + def findallPath(n,k,sum,startIndex): + if sum > n: return #剪枝操作 + if sum == n and len(path) == k: #如果path.size() == k 但sum != n 直接返回 + return res.append(path[:]) + for i in range(startIndex,9-(k-len(path))+2): #剪枝操作 + path.append(i) + sum += i + findallPath(n,k,sum,i+1) #注意i+1调整startIndex + sum -= i #回溯 + path.pop() #回溯 + + findallPath(n,k,0,1) + return res +``` Go: