-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.py
More file actions
25 lines (16 loc) · 824 Bytes
/
Permutations.py
File metadata and controls
25 lines (16 loc) · 824 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
result = []
for i in nums:
#base case if len is 1 then, return the nums
if (len(nums) == 1):
return [nums[:]]
#if not base case, pop the first element and call permute on the rest of the nums array
n = nums.pop(0)
perms = self.permute(nums)
#for each oermutations in each level, append the n
for p in perms:
p.append(n)
result.extend(perms) #extend add multiple items to the list
nums.append(n) #this adds the n back to the nums for next iteration
return result