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] ]先排序,在深搜,深搜的时候用pre记录前一个数。
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
nums.sort()
self.dfs(res, [], None, nums)
return res
def dfs(self, res, theList, pre, nums):
if len(nums) == 0:
res.append(list(theList))
pre = None
for i in xrange(len(nums)):
if nums[i] == pre:
continue
theList.append(nums[i])
self.dfs(res, theList, nums[i], nums[:i] + nums[i+1:])
theList.pop()
pre = nums[i]