My simple non-recursive Python solution


  • 0
    S
    class Solution:
        # @return a list of lists of integers
        def combine(self, n, k):
            combo = [[]]
            for i in range(1, n+1):
              new_list = [j[:] for j in combo if len(j) < k]
              for j in new_list:
                j.append(i)
              combo.extend(new_list)
            return [j for j in combo if len(j) == k]

Log in to reply
 

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