My simple python solution for distinct permutation


  • 0
    Z

    When I first saw the problem, somehow my brain believed the point is to remove duplicated results..

    Anyway this is my code:

    def permute(self, nums):
        if not nums: return []
        nums.sort()
        results = []
        self.perm(nums, [], results)
        return results
    
    def perm(self, nums, result, results):
        prev = []
        for num in nums:
            if prev != num:
                prev = num
                newnums = nums[:]
                newnums.remove(num)
                if len(newnums) > 0:
                    self.perm(newnums, result+[num], results)
                else:
                    results.append(result+[num])
                    return
    

    I found that most of other posted python solution will run for a long time, in input like [1,0,0,0,0,0,0,0,0,0,0]

    Well if the answer with duplications is required, I can simply duplicate my output, for example, 10! time in above case that has 10 zeros.


Log in to reply
 

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