diff --git a/46-permutations/46-permutations.py b/46-permutations/46-permutations.py new file mode 100644 index 0000000..f87fe6b --- /dev/null +++ b/46-permutations/46-permutations.py @@ -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, []) \ No newline at end of file