Merge pull request #244 from jojoo15/patch-22

添加 0216.组合总数 python3版本
This commit is contained in:
Carl Sun
2021-05-25 18:18:11 +08:00
committed by GitHub

View File

@ -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