Simple python 84ms DFS


  • 0
    F
    class Solution(object):
    def combine(self, n, k):
        res=[]
        self.cal([i for i in xrange(1,n+1)], k, [], res)
        return res
        
    def cal(self, lst, k, temp, res):
        if k>len(lst): return
        if k==0: res+=temp,
        for i in xrange(len(lst)): self.cal(lst[i+1:], k-1, temp+[lst[i]], res)

Log in to reply
 

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