Skip to content

Commit 63be339

Browse files
authored
添加 0216.组合总数 python3版本
添加 0216.组合总数 python3版本
1 parent e9f8cda commit 63be339

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

problems/0216.组合总和III.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,25 @@ class Solution {
262262
```
263263

264264
Python:
265-
265+
```python3
266+
class Solution:
267+
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
268+
res = [] #存放结果集
269+
path = [] #符合条件的结果
270+
def findallPath(n,k,sum,startIndex):
271+
if sum > n: return #剪枝操作
272+
if sum == n and len(path) == k: #如果path.size() == k 但sum != n 直接返回
273+
return res.append(path[:])
274+
for i in range(startIndex,9-(k-len(path))+2): #剪枝操作
275+
path.append(i)
276+
sum += i
277+
findallPath(n,k,sum,i+1) #注意i+1调整startIndex
278+
sum -= i #回溯
279+
path.pop() #回溯
280+
281+
findallPath(n,k,0,1)
282+
return res
283+
```
266284

267285
Go:
268286

0 commit comments

Comments
 (0)