my Python solution: shorter and simpler logic than top rated


  • 0
    C

    skipping duplicates, that's why i sorted first

    class Solution(object):
        def permuteUnique(self, nums):
            nums = sorted(nums)
            res = []
            self.helper(nums, res, [], len(nums))
            return res
        def helper(self, nums, res, sub, size):
            if len(sub) == size:
                res.append(sub)
            for i in range(len(nums)):
                if i + 1 < len(nums) and nums[i] == nums[i + 1]:
                    continue
                else:
                    self.helper(nums[:i]+nums[i + 1:], res, sub+[nums[i]], size)
    ``

Log in to reply
 

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