Tar2

Untitled

Oct 15th, 2019
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. # Leetcodde problem - https://leetcode.com/problems/permutations/
  2. perms = []
  3. def permHelper(nums, chosen):
  4.         # print('permHelper(', nums , ', ' , chosen , ')', sep = "")
  5.         # if-else
  6.         if len(nums) == 0:
  7.             print(chosen)               # this line prints the permutation correctly
  8.             perms.append(chosen)        # but perms returning list of empty lists!
  9.         else:
  10.             for i in range(len(nums)):
  11.                 # choose
  12.                 n = nums[i]
  13.                 nums.remove(n)
  14.                 chosen.append(n)
  15.            
  16.                 # explore
  17.                 permHelper(nums, chosen)
  18.                
  19.                 # un-choose
  20.                 nums.insert(i, n)
  21.                 chosen.remove(n)
  22.         # return perms
  23.    
  24. class Solution:
  25.     def permute(self, nums: List[int]) -> List[List[int]]:
  26.         permHelper(nums, [])
  27.         return perms
Add Comment
Please, Sign In to add comment