from typing import List class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: def dfs(nums, size, depth, path, res, used): if depth == size: if path not in res: res.append(path[:]) return for i in range(size): if not used[i]: used[i] = True path.append(nums[i]) dfs(nums, size, depth + 1, path, res, used) used[i] = False path.pop() size = len(nums) if len(nums) == 0: return [] used = [False for _ in range(size)] res = [] dfs(nums, size, 0, [], res, used) return res if __name__ == "__main__": nums = [1, 1, 3] solution = Solution() res = solution.permuteUnique(nums) print(res)