forked from algorhythms/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
046 Permutations II.py
52 lines (43 loc) · 1.55 KB
/
046 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
43
44
45
46
47
48
49
50
51
52
"""
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].
"""
__author__ = 'Danyang'
class Solution:
def permuteUnique_TLE(self, num):
"""
list to set
Time Limit Exceeded
:param num: a list of integer
:return: a list of lists of integers
"""
result = []
self.get_permute(num, [], result)
return map(list, set(map(tuple, result)))
def get_permute_TLE(self, nums, current, result):
length = len(nums)
if length==0:
result.append(current)
for ind, val in enumerate(nums):
self.get_permute(nums[:ind]+nums[ind+1:], current+[val], result)
def permuteUnique(self, num):
"""
Jump
compared to 045 Permutation, two lines added: sort and continue
:param num: a list of integer
:return: a list of lists of integers
"""
result = []
num.sort()
self.get_permute(num, [], result)
return result
def get_permute(self, nums, current, result):
if not nums:
result.append(current)
for ind, val in enumerate(nums):
if ind-1>=0 and val==nums[ind-1]: continue # JUMP; only need to compare to previous value
self.get_permute(nums[:ind]+nums[ind+1:], current+[val], result)
if __name__=="__main__":
print Solution().permuteUnique([1, 1, 2])