代码随想录算法训练营第二十四天
77.组合
代码
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2022.11
# @Author : hello algorithm!
# @Note : https://leetcode.cn/problems/combinations/description/
from typing import List
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res = []
path = []
def backtrack(n, k, StartIndex):
if len(path) == k:
res.append(path[:])
return
for i in range(StartIndex, n + 1):
path.append(i)
backtrack(n, k, i + 1)
path.pop()
backtrack(n, k, 1)
return res
if __name__ == '__main__':
n = 2
k = 2
s = Solution()
print(s.combine(n, k))