46. Permutations

Medium

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

😇 Solution

class Solution:
    def permutation(lst,x):
        res = []
        if len(lst) < 1:
            temp = [x]
            res.append(temp)
            return res

        for l in lst:
            for i in range(len(l)+1):
                temp = l[:]
                temp.insert(i,x)
                res.append(temp)
        return res
    
    def permute(self, nums: List[int]) -> List[List[int]]:
        
        if not nums:
            return nums

        temp = []
        for n in nums:
            temp = Solution.permutation(temp,n)
        
        return temp

Last updated

Was this helpful?