Simple and Clean Python solution


  • 0
    class Solution(object):
        def permuteUnique(self, nums):
            def combinations(nums, path, res):
                if not nums:
                    res.append(path)
                    return
                i, n = 0, len(nums)
                while i < n:
                    while 0 < i < n and nums[i] == nums[i - 1]:
                        i += 1
                    if i < n:
                        c = nums.pop(i)
                        combinations(nums, path + [c], res)
                        nums.insert(i, c)
                    i += 1
                return
            
            path, res = [], []
            nums.sort()
            combinations(nums, path, res)
            return res

Log in to reply
 

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