77.组合
题目链接:https://leetcode.cn/problems/combinations/
回溯最关键的就是剪枝
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res = []
path = []
def backtracking(n, k, startindex):
if len(path) == k:
res.append(path[:])
return
for i in range(startindex, n-(k-len(path))+2):
path.append(i)
backtracking(n,k,i+1)
path.pop()
backtracking(n,k,1)
return res