题目链接
https://leetcode.cn/problems/combinations/description/
代码
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
result = []
return self.backtracking(n, k, 1, [], result)
def backtracking(self, n, k, startIndex, path, result):
if len(path) == k:
result.append(path[:])
for i in range(startIndex, n - (len(path)) + 2):
path.append(i)
self.backtracking(n, k, i + 1, path, result)
path.pop()
return result