Skip to content

Commit 9aca025

Browse files
authored
Create 46-Permutations.py
1 parent 8ceb091 commit 9aca025

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

46-Permutations.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def permute(self, nums: List[int]) -> List[List[int]]:
3+
res = []
4+
5+
# base case
6+
if len(nums) == 1:
7+
return [nums[:]] # nums[:] is a deep copy
8+
9+
for i in range(len(nums)):
10+
n = nums.pop(0)
11+
perms = self.permute(nums)
12+
13+
for perm in perms:
14+
perm.append(n)
15+
res.extend(perms)
16+
nums.append(n)
17+
return res

0 commit comments

Comments
 (0)