- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path46-Permutations.py
30 lines (22 loc) · 642 Bytes
/
46-Permutations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'''Leetcode- https://leetcode.com/problems/permutations/'''
'''
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
'''
defpermute(nums):
result= []
# base case
iflen(nums) ==1:
return [nums.copy()] #ornums[:] is a deep copy
foriinrange(len(nums)):
n=nums.pop()
perms=permute(nums)
forperminperms:
perm.append(n)
result.extend(perms)
nums.append(n)
returnresult
#T:O(2^n)
#S:O(n)