【Leetcode】47—PermutationsII

2019-08-04  本文已影响0人  Gaoyt__
一、题目描述

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

输入: [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
上一篇下一篇

猜你喜欢

热点阅读