Skip to content

Latest commit

 

History

History
30 lines (24 loc) · 658 Bytes

47.md

File metadata and controls

30 lines (24 loc) · 658 Bytes

47. Permutations II

输出一个有重复数字的数组的全排列。

思路: dfs

class Solution:
    def permuteUnique(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        # 6 star, 排序,然后dfs,需要去重
        nums.sort()
        rs = []
        self.dfs(nums, [], rs)
        return rs

    def dfs(self, nums, path, rs):
        if nums == []:
            rs.append(path)
            return
        for i in range(len(nums)):
            if i > 0 and nums[i] == nums[i-1]:
                continue
            self.dfs(nums[:i]+nums[i+1:], path+[nums[i]], rs)