Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]

My code:

class Solution:
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        all_lists=self.sub_permute(nums)
        return all_lists
    def sub_permute(self,nums):
        if len(nums)==1:
            return [[nums[0]]]
        lists=[]
        for item in nums:
            tmp_nums=nums.copy()
            tmp_nums.remove(item)
            tmp_list=self.sub_permute(tmp_nums)
            for item1 in tmp_list:
                tmp_use_list=[]
                tmp_use_list.append(item)
                tmp_use_list=tmp_use_list+item1
                lists.append(tmp_use_list)
        return lists
        

Solution: https://leetcode.com/problems/permutations/