Permutations II
Question
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]
]
Tags
- Backtracking
Thought
The solution is very similar to the Permutations problem. However, since there are duplicated elements in the array, it is necessary to sort the array and skip the duplicated elements in the loop.
Code
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) == 0:
return []
if len(nums) == 1:
return [nums]
nums.sort()
res = []
for i in xrange(len(nums)):
if i > 0 and nums[i] == nums[i - 1]:
continue
for j in self.permuteUnique(nums[:i] + nums[i + 1:]):
res.append([nums[i]] + j)
return res