10 lines of code in Python


  • 0
    H
    class Solution(object):
        def combine(self, n, k):
            if k == 0:
                return []
            elif k == 1:
                return [[i] for i in range(1, n + 1)]
            elif n == k:
                return [[i for i in range(1, n + 1)]]
            else:
                return self.combine(n - 1, k) + map(lambda x: x + [n], self.combine(n - 1, k - 1))
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.