【Leetcode】47—PermutationsII

一、题目描述

给定一个可包含重复数字的序列,返回所有不重复的全排列。
示例:

输入: [1,1,2]
输出:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]
二、代码实现
class Solution(object):
    def permuteUnique(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        finallen = len(nums)
        
        def dfs(nums, cur, res):
            if len(cur) == finallen: 
                if cur in res: return
                res.append(cur)
                return
            
            for index, num in enumerate(nums):
                dfs(nums[:index] + nums[index+1:], cur + [num], res)
                       
        res = []
        cur = []
        
        for index, num in enumerate(nums):
            dfs(nums[:index] + nums[index+1:], cur + [num], res)
        
        return res

你可能感兴趣的:(【Leetcode】47—PermutationsII)