基于回溯法的交换
为了避免重复,每次创建一个集合来统计当前交换数是否已经交换过,交换过就跳过

#
# 
# @param num int整型一维数组 
# @return int整型二维数组
#
class Solution:
    def backtrack(self, num, level, ans):
        if level == len(num):
            ans.append(num[:])
            return 
        seen = set()
        for i in range(level, len(num)):
            if num[i] in seen:
                continue
            seen.add(num[i])
            num[i], num[level] = num[level], num[i]
            self.backtrack(num, level + 1, ans)
            num[i], num[level] = num[level], num[i]

    def permuteUnique(self , num ):
        # write code here
        ans = []
        self.backtrack(num, 0, ans)
        return ans