Python DFS + remove duplications


  • 0
    X
    class Solution(object):
        def permuteUnique(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            def dfs(nums,value):
                if len(value) == size:
                    ans.append(value)
                    return
                for i in range(len(nums)):
                    if i and nums[i] == nums[i-1]:
                        continue
                    dfs(nums[:i]+nums[i+1:],value+[nums[i]])
                    
            ans = []
            if not nums: return ans
            size = len(nums)
            nums.sort()
            dfs(nums,[])
            return ans

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.