题目:
题目链接: https://leetcode-cn.com/problems/combinations/
解题思路:
"所有组合",回溯 + 剪枝
代码实现:
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
def create_res(n, k, res, path):
if k == 0:
res.append(path[:])
return
for n in range(n, 0, -1):
path.append(n)
create_res(n - 1, k - 1, res, path)
path.pop()
res = []
create_res(n, k, res, [])
return res