HTMLify

LeetCode - Permutations II - Python
Views: 12 | Author: abh
class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        combs = []
        if len(nums) == 1:
            return [nums]
        if len(nums) == 2:
            c1 = [nums[0], nums[1]]
            c2 = [nums[1], nums[0]]
            combs.append(c1)
            if c1 != c2:
                combs.append(c2)
            return combs
        for i in nums:
            t_nums = nums.copy()
            t_nums.remove(i)
            s_combs = self.permuteUnique(t_nums)
            for s_comb in s_combs:
                comb = [i] + s_comb
                if comb not in combs:
                    combs.append(comb)
        return combs

Comments