-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path46.permutations.py
45 lines (39 loc) · 1.15 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#
# @lc app=leetcode id=46 lang=python3
#
# [46] Permutations
#
# @lc code=start
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
"""
Using Library
T : O(N!) 24.10% | 73ms
S : O(N) 83.80% | 13.9mb
"""
import itertools
return list(itertools.permutations(nums))
class Solution:
"""
Level0: []
level1: [1] [2] [3]
level2: [1,2] [1,3] [2,1] [2,3] [3,1] [3,2]
level3: [1,2,3] [1,3,2] [2,1,3][2,3,1] [3,1,2][3,2,1]
"""
def permute(self, nums: List[int]) -> List[List[int]]:
visited = set()
res = []
self.backtracking(res, visited, [], nums)
return res
def backtracking(self, res, visited, subset, nums):
if len(subset) == len(nums):
res.append(subset)
for i in range(len(nums)):
if i not in visited:
# Save
visited.add(i)
# Backtrack
self.backtracking(res, visited, subset+[nums[i]], nums)
# Remove
visited.remove(i)
# @lc code=end