-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_047_Permutations2.py
More file actions
32 lines (27 loc) · 975 Bytes
/
_047_Permutations2.py
File metadata and controls
32 lines (27 loc) · 975 Bytes
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
#-----------------------------------------------------------------------------
# Runtime: 56ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
class Solution:
def permuteUnique(self, nums: [int]) -> [[int]]:
if len(nums) == 1:
return [ nums ]
nums.sort()
visited = [False] * len(nums)
result = []
temp_result = []
def dfs(temp_result: [int]):
if len(nums) == len(temp_result):
result.append(temp_result.copy())
return
for i in range(len(nums)):
if visited[i] or (i > 0 and nums[i] == nums[i - 1] and not visited[i - 1]):
continue
temp_result.append(nums[i])
visited[i] = True
dfs(temp_result)
visited[i] = False
temp_result.pop()
dfs(temp_result)
return result