Time: 47 ms (63.61%), Space: 14.1 MB (56.56%) - LeetHub

This commit is contained in:
Deven
2023-01-13 19:52:50 -05:00
parent a2520724f8
commit 043507e55e
+15
View File
@@ -0,0 +1,15 @@
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
def permute_with_prefix(nums: List[int], prefix: List[int]):
# Base case
if len(nums) == 1:
return [prefix + nums]
permutations = []
for i in range(len(nums)):
permutations += permute_with_prefix(nums[:i] + nums[(i + 1):], # Remove the ith element from nums
prefix + [nums[i]])
return permutations
# Initial call
return permute_with_prefix(nums, [])