【leetcode系列】【算法】【中等】组合

题目:

题目链接: https://leetcode-cn.com/problems/combinations/

解题思路:

"所有组合",回溯 + 剪枝

代码实现:

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        def create_res(n, k, res, path):
            if k == 0:
                res.append(path[:])
                return

            for n in range(n, 0, -1):
                path.append(n)
                create_res(n - 1, k - 1, res, path)
                path.pop()
                
        res = []
        create_res(n, k, res, [])
        return res
发布了100 篇原创文章 · 获赞 4 · 访问量 1486

猜你喜欢

转载自blog.csdn.net/songyuwen0808/article/details/105258163
今日推荐