-
Notifications
You must be signed in to change notification settings - Fork 20
/
PermutationsII.py
44 lines (37 loc) · 967 Bytes
/
PermutationsII.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
# -*- coding: UTF-8 -*-
#
# Given a collection of numbers that might contain duplicates, return all possible unique permutations.
#
# For example,
# [1,1,2] have the following unique permutations:
# [
# [1,1,2],
# [1,2,1],
# [2,1,1]
# ]
#
# Python, Python 3 all accepted.
class PermutationsII:
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
results = []
length = len(nums)
if length == 0:
return results
if length == 1:
results.append([nums[0]])
return results
ints = nums[0:length - 1]
m_set = set()
for l in self.permuteUnique(ints):
for i in range(len(l) + 1):
tmp = []
tmp.extend(l)
tmp.insert(i, nums[length - 1])
tp = tuple(tmp)
m_set.add(tp)
results.extend(m_set)
return results