python leetcode 77. Combinations

最基本的DFS思想

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

你可能感兴趣的:(leetcode,python)