-
Notifications
You must be signed in to change notification settings - Fork 2
/
47. Permutations II.py
42 lines (36 loc) · 993 Bytes
/
47. Permutations II.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
# Given a collection of numbers that might contain duplicates,
# return all possible unique permutations.
# Example:
# Input: [1,1,2]
# Output:
# [
# [1,1,2],
# [1,2,1],
# [2,1,1]
# ]
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
temp = []
visited = [False] * len(nums)
# 1
self.dfs(sorted(nums), 0, temp, res, visited)
return res
def dfs(self, nums, pos, temp, res, visited):
if pos == len(nums):
res.append(temp[:])
return
for i in range(len(nums)):
# 2
if i > 0 and nums[i] == nums[i-1] and visited[i-1]:
continue
if not visited[i]:
visited[i] = True
temp.append(nums[i])
self.dfs(nums, pos+1, temp, res, visited)
visited[i] = False
temp.pop()