Python iterative solution

  • 0
    class Solution(object):
    def combine(self, n, k):
        res, stack=[], [([i for i in xrange(1,n+1)], k, [])]
        while stack:
            lst, k, temp=stack.pop()
            if k<=len(lst):
                if not k: res+=temp,
                for i in xrange(len(lst)): stack+=(lst[i+1:],k-1,temp+[lst[i]]),
        return res

Log in to reply

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