-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_47.py
38 lines (26 loc) · 941 Bytes
/
prob_47.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
# 47. Permutations II
# https://leetcode.com/problems/permutations-ii/
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if nums == [] or not nums:
return [[]]
nums = sorted(nums)
result = []
res = nums[:]
self.helper(nums, res, [], result)
return result
def helper(self, nums, res, tmp_list, result):
if len(tmp_list) == len(nums):
result.append(tmp_list[:])
for index in range(0, len(res)):
if index > 0 and res[index] == res[index-1]:
continue
tmp_list.append(res[index])
tmp_res = res[:]
tmp_res.pop(index)
self.helper(nums, tmp_res, tmp_list, result)
tmp_list.pop()