[77][中等][回溯] 组合
题目描述
输入: n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]解题思路
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
results = []
def dfs(path, start):
if len(path) == k:
results.append(path[:])
for i in range(start, n + 1):
path.append(i)
dfs(path, i + 1)
path.pop()
dfs([], 1)
return results最后更新于