forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutations-ii.py
34 lines (26 loc) · 927 Bytes
/
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
# Time: O(n!)
# Space: O(n)
#
# 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], and [2,1,1].
#
class Solution:
# @param num, a list of integer
# @return a list of lists of integers
def permuteUnique(self, nums):
solutions = [[]]
for num in nums:
next = []
for solution in solutions:
for i in xrange(len(solution) + 1):
candidate = solution[:i] + [num] + solution[i:]
if candidate not in next:
next.append(candidate)
solutions = next
return solutions
if __name__ == "__main__":
print Solution().permuteUnique([1, 1, 2])
print Solution().permuteUnique([1, -1, 1, 2, -1, 2, 2, -1])