mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 47 ms (63.61%), Space: 14.1 MB (56.56%) - LeetHub
This commit is contained in:
@@ -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, [])
|
||||||
Reference in New Issue
Block a user