From ea92a02dd05e098e8183379cd75489aff38495e1 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Sun, 23 May 2021 21:31:51 +0200 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=200077.=E7=BB=84=E5=90=88=20?= =?UTF-8?q?python3=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改 0077.组合 python3版本 --- problems/0077.组合.md | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/problems/0077.组合.md b/problems/0077.组合.md index 783f7da7..003373d9 100644 --- a/problems/0077.组合.md +++ b/problems/0077.组合.md @@ -370,23 +370,21 @@ class Solution { Python: -```python +```python3 class Solution: - result: List[List[int]] = [] - path: List[int] = [] def combine(self, n: int, k: int) -> List[List[int]]: - self.result = [] - self.combineHelper(n, k, 1) - return self.result - - def combineHelper(self, n: int, k: int, startIndex: int): - if (l := len(self.path)) == k: - self.result.append(self.path.copy()) - return - for i in range(startIndex, n - (k - l) + 2): - self.path.append(i) - self.combineHelper(n, k, i + 1) - self.path.pop() + res=[] #存放符合条件结果的集合 + path=[] #用来存放符合条件结果 + def backtrack(n,k,startIndex): + if len(path) == k: + res.append(path[:]) + return + for i in range(startIndex,n+1): + path.append(i) #处理节点 + backtrack(n,k,i+1) #递归 + path.pop() #回溯,撤销处理的节点 + backtrack(n,k,1) + return res ``` javascript ```javascript