알고리즘

[LeetCode] 77. Combinations

유병각 2022. 4. 28. 23:39

77. Combinations

풀이_#1

재귀를 돌려서 풀었슴!

import copy

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        r = []

        visited = [0] * (n + 1)

        def rec(l):
            if (len(l) == k):
                r.append(copy.deepcopy(l))
                return ;

            nowNum = l[-1]

            for i in range(nowNum + 1, n+1):
                l.append(i)
                rec(l)
                l.remove(i)

        for i in range(1, n + 2 - k):
            l = [i]
            rec(l)
            l.remove(i)

        return r