A simple, easy-to-read python solution


  • 0
    T

    notice that I first convert input to a List[ List[int] ] structure to generalize the code to be suitable not only for unique ordered sequence of int, but also all kind of input as long as all elements are guaranteed to be unique.

    class Solution(object):
        def combine(self, n, k):
            self.solution_set = []
            self.combineHelper([[i] for i in range(1, n+1)], [], -1, k)
            return self.solution_set
    
        def combineHelper(self, elements, sub_sol, last_pos, k):
            if k == 0:
                self.solution_set.append(sub_sol)
                return
            for i in range(last_pos+1, len(elements) - k + 1):
                self.combineHelper(elements, sub_sol+elements[i], i, k-1)
    

Log in to reply
 

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