Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.


Input: [1,1,2]
class Solution:
    def permuteUnique(self, nums: List[int]) -> List[List[int]]:
        self.result = []

        def perm(data, begin, end):
            if begin == end:
                self.result += [[item for item in data]]
                i = begin
                for j in range(begin, end):
                    data[i], data[j] = data[j], data[i]
                    perm(data, begin + 1, end)
                    data[i], data[j] = data[j], data[i]
        perm(nums, 0, len(nums))

        return self.result

Leave a Reply

Your email address will not be published.