whole_arrang_two.py 895 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from typing import List
  2. class Solution:
  3. def permuteUnique(self, nums: List[int]) -> List[List[int]]:
  4. def dfs(nums, size, depth, path, res, used):
  5. if depth == size:
  6. if path not in res:
  7. res.append(path[:])
  8. return
  9. for i in range(size):
  10. if not used[i]:
  11. used[i] = True
  12. path.append(nums[i])
  13. dfs(nums, size, depth + 1, path, res, used)
  14. used[i] = False
  15. path.pop()
  16. size = len(nums)
  17. if len(nums) == 0:
  18. return []
  19. used = [False for _ in range(size)]
  20. res = []
  21. dfs(nums, size, 0, [], res, used)
  22. return res
  23. if __name__ == "__main__":
  24. nums = [1, 1, 3]
  25. solution = Solution()
  26. res = solution.permuteUnique(nums)
  27. print(res)