Skip to content

Commit

Permalink
Create 46-Permutations.py
Browse files Browse the repository at this point in the history
  • Loading branch information
neetcode-gh authored Mar 28, 2022
1 parent 8ceb091 commit 9aca025
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 46-Permutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = []

# base case
if len(nums) == 1:
return [nums[:]] # nums[:] is a deep copy

for i in range(len(nums)):
n = nums.pop(0)
perms = self.permute(nums)

for perm in perms:
perm.append(n)
res.extend(perms)
nums.append(n)
return res

0 comments on commit 9aca025

Please sign in to comment.