-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47. Permutations II.py
More file actions
38 lines (32 loc) · 924 Bytes
/
47. Permutations II.py
File metadata and controls
38 lines (32 loc) · 924 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
33
34
35
36
37
38
'''
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:
def permuteUnique(self, nums):
if len(nums)==0:
return []
result = []
nums.sort()
return self.recursive(nums,0,result)
def recursive(self, nums, start, result):
myset = set()
if start>=len(nums)-1:
#print(start,nums)
result.append(nums[:])
return result
for i in range(start,len(nums)):
#print(start,i)
if nums[i] not in myset:
myset.add(nums[i])
nums[start], nums[i] = nums[i], nums[start]
self.recursive(nums,start+1,result)
nums[start], nums[i] = nums[i], nums[start]
return result