LeetCode-77-Combinations dfs+剪枝


class Solution(object):
    ans=[]
    cur=[]
    def combine(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: List[List[int]]
        """
        self.ans=[]
        self.cur=[]
        self.dfs(k,1,n)
        return self.ans
    def dfs(self,num,start,n):
        if num==0:
            self.ans.append(self.cur[:])
            return
        for i in range(start,n+1-num+1):
            self.cur.append(i)
            self.dfs(num-1,i+1,n)
            self.cur.pop()


你可能感兴趣的:(Leetcode)