LeetCode(力扣)77. 组合Python

LeetCode77. 组合

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/combinations/description/
LeetCode(力扣)77. 组合Python_第1张图片

代码

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

你可能感兴趣的:(leetcode,python,算法,职场和发展)