Python3 Solution By Using Backtracking


  • 0
    X

    Python3 Solution By Using Backtracking:
    '''
    class Solution:
    def permute(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    res = []
    self.dfs(res, [], nums, 0)
    return(res)

    def dfs(self, res, curres, curcan, level):
        if len(curcan) == 0:
            res += [list(curres)]
            return()
        for i in range(0, len(curcan)):
            #print(' '*level+'i='+str(i), 'level='+str(level), 'curres='+str(curres), 'curcan='+str(curcan), 'res='+str(res))
            curlast = curcan.pop(0)
            self.dfs(res, curres+[curlast], curcan, i+1)
            curcan.append(curlast)
    

    '''


Log in to reply
 

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