From 8270c724845a1c1612127a72406c8249412c05c9 Mon Sep 17 00:00:00 2001 From: jojoo15 <75017412+jojoo15@users.noreply.github.com> Date: Sun, 23 May 2021 22:18:48 +0200 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200077.=E7=BB=84=E5=90=88?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20python=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0077.组合优化 python版本 --- problems/0077.组合优化.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/problems/0077.组合优化.md b/problems/0077.组合优化.md index 3f6c2fce..0fb11e53 100644 --- a/problems/0077.组合优化.md +++ b/problems/0077.组合优化.md @@ -176,8 +176,22 @@ class Solution { ``` Python: - - +```python3 +class Solution: + def combine(self, n: int, k: int) -> List[List[int]]: + res=[] #存放符合条件结果的集合 + path=[] #用来存放符合条件结果 + def backtrack(n,k,startIndex): + if len(path) == k: + res.append(path[:]) + return + for i in range(startIndex,n-(k-len(path))+2): #优化的地方 + path.append(i) #处理节点 + backtrack(n,k,i+1) #递归 + path.pop() #回溯,撤销处理的节点 + backtrack(n,k,1) + return res +``` Go: