Share my Python solution with recuision


  • 0
    Z
        def permuteUnique(self, nums):
            """
            :type nums: List[int]
            :rtype: List[List[int]]
            """
            d = {}
            for n in nums:
                if not n in d:
                    d[n] = 1
                else:
                    d[n] = d[n] + 1
            ret = []
            def helper(prefix):
                isLast = True
                for dd in d.keys():
                    if d[dd] != 0:
                        d[dd] -= 1
                        new_prefix = prefix[:]
                        new_prefix.append(dd)
                        helper(new_prefix)
                        d[dd] += 1
                        isLast = False
                if isLast:
                    ret.append(prefix)
            prefix = []
            helper(prefix)
            return ret
    

Log in to reply
 

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