Share Python solution without using set


  • 6
    C

    Same idea as already in other posts, just the Python version for reference.

    def permuteUnique(self, num):
        if not num:
            return []
        return self.permute(sorted(num))
        
    def permute(self, num):
        if len(num) == 1:
            return [num]
            
        ret = []
        for index, elt in enumerate(num):
            if index > 0 and num[index - 1] == elt:
                continue
            ret += [[elt] + p for p in self.permute(num[:index] + num[index + 1:])]
        return ret

Log in to reply
 

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