smj007

Untitled

Jun 21st, 2022
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. class Solution:
  2.     def permute(self, nums: List[int]) -> List[List[int]]:
  3.        
  4.         # Base Case
  5.         if not len(nums):
  6.             return [[]]
  7.        
  8.         result = []
  9.         for i in range(len(nums)):
  10.            
  11.             # let's assume it returns a list of lists
  12.             # of all possible permutations except the
  13.             # element in question/iterating over
  14.             permutations = self.permute(nums[:i] + nums[i+1:])
  15.            
  16.             # add the element in question as a leading element
  17.             # to the generated permutations and add each list
  18.             # to the final result
  19.             for j in range(len(permutations)):
  20.                 permutations[j] = [nums[i]] + permutations[j]
  21.            
  22.             # Gather final result and add list of lists to the
  23.             # to the accumulator result variable
  24.             result += permutations
  25.                
  26.         return result
  27.            
Advertisement
Add Comment
Please, Sign In to add comment