mycode 没有通过,其实只需要把temp.append改为temp+[nums[i]]即可

def permute(nums):
    def dfs(res,nums,temp):
            print(nums,temp)
            if temp:
                if len(temp) == numslen:
                     res.append(temp)
            for i in range(numslen):
                print('..',i,temp)
                temp = temp.append(nums[i])
                print(temp)
                dfs(res,nums[:i]+nums[i+1:],temp)
    if not nums:
        return []
    res = []
    numslen = len(nums)
    dfs(res,nums,[])
    return res
        
permute([1,2,3])

 

 

参考

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        def dfs(nums,temp):
            if len(nums)==0:
                self.res.append(temp)
            for i in range(len(nums)):
                dfs(nums[:i]+nums[i+1:],temp+[nums[i]])
        self.res = []
        dfs(nums,[])
        return self.res