a regular backtrack method use python


  • 0
    N
    class Solution(object):
        def permuteUnique(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            ret_list = []
            if not nums:
                return ret_list
            nums.sort()
            self.dfsGetPermute(nums, [], ret_list, 0)
            
            return ret_list
        
        def dfsGetPermute(self, nums, temp_list, ret_list, start):
            if len(temp_list) == len(nums):
                ret_list.append(temp_list)
                return
            
            used = {}
            for i in xrange(start, len(nums)):
                if used.has_key(nums[i]):
                    continue
                used[nums[i]] = True
                self.swapNumbers(nums, start, i)
                self.dfsGetPermute(nums, temp_list + [nums[start]], ret_list, start + 1)
                self.swapNumbers(nums, start, i)
        
        def swapNumbers(self, nums, index1, index2):
            temp = nums[index1]
            nums[index1] = nums[index2]
            nums[index2] = temp
    

Log in to reply
 

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