题目

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

Example:

  1. Input: [1,1,2]
  2. Output:
  3. [
  4. [1,1,2],
  5. [1,2,1],
  6. [2,1,1]
  7. ]

题意

求给定数组的全排列,注意数组中有重复元素。

思路

0046. Permutations (M) 相比,多了重复元素,只要在46解法基础上加上相应的限制即可。

  1. 回溯法:
    • 使用hash:用一张hash表记录整个搜索中对应下标的数是否已被使用,每一层递归中再用一张hash表记录当前递归层次中已经插入过(又移除)的数字,每次操作都往已有序列后插入一个未被使用的数(既是整个搜索中未被使用,也是当前递归中未被使用),更新hash,递归,再移除改数,更新hash。
    • 不使用hash:对于结果序列中的每一个位置index,它可能存放的数为nums从index到最后一个位置nums.length-1中的任意一个数,但要保证每次存放的都是不同的数。所以每次操作都从这(nums.length - index)个数中选出一个,并判断该数是否已经存放过,若没有则放到当前空位,再对右边的空位进行递归操作。
  2. 结合 0031. Next Permutation (M) 来实现全排列。

代码实现

Java

回溯法无hash

  1. class Solution {
  2. public List<List<Integer>> permuteUnique(int[] nums) {
  3. List<List<Integer>> ans = new ArrayList<>();
  4. permute(nums, 0, ans);
  5. return ans;
  6. }
  7. private void permute(int[] nums, int index, List<List<Integer>> ans) {
  8. if (index == nums.length - 1) {
  9. List<Integer> list = new ArrayList<>();
  10. for (int i = 0; i < nums.length; i++) {
  11. list.add(nums[i]);
  12. }
  13. ans.add(list);
  14. return;
  15. }
  16. for (int i = index; i < nums.length; i++) {
  17. // 每次都要判断该数是否已经使用过
  18. boolean flag = false;
  19. for (int j = index; j < i; j++) {
  20. if (nums[i] == nums[j]) {
  21. flag = true;
  22. break;
  23. }
  24. }
  25. if (flag) {
  26. continue;
  27. }
  28. swap(nums, index, i);
  29. permute(nums, index + 1, ans);
  30. swap(nums, index, i);
  31. }
  32. }
  33. private void swap(int[] nums, int i, int j) {
  34. int temp = nums[i];
  35. nums[i] = nums[j];
  36. nums[j] = temp;
  37. }
  38. }

回溯法hash

  1. class Solution {
  2. public List<List<Integer>> permuteUnique(int[] nums) {
  3. List<List<Integer>> ans = new ArrayList<>();
  4. permute(nums, new boolean[nums.length], new ArrayList<>(), ans);
  5. return ans;
  6. }
  7. private void permute(int[] nums, boolean[] hash, List<Integer> list, List<List<Integer>> ans) {
  8. if (list.size() == nums.length) {
  9. ans.add(new ArrayList<>(list));
  10. }
  11. // 当前递归中也要用一张hash表记录已经插入过的数字
  12. Set<Integer> used = new HashSet<>();
  13. for (int i = 0; i < nums.length; i++) {
  14. if (!hash[i] && !used.contains(nums[i])) {
  15. used.add(nums[i]);
  16. list.add(nums[i]);
  17. hash[i] = true;
  18. permute(nums, hash, list, ans);
  19. hash[i] = false;
  20. list.remove(list.size() - 1);
  21. }
  22. }
  23. }
  24. }

nextPermutation

  1. class Solution {
  2. public List<List<Integer>> permuteUnique(int[] nums) {
  3. List<List<Integer>> ans = new ArrayList<>();
  4. Arrays.sort(nums);
  5. while (true) {
  6. List<Integer> list = new ArrayList<>();
  7. for (int i = 0; i < nums.length; i++) {
  8. list.add(nums[i]);
  9. }
  10. ans.add(list);
  11. if (hasNextPermutation(nums)) {
  12. nextPermutation(nums);
  13. } else {
  14. break;
  15. }
  16. }
  17. return ans;
  18. }
  19. // 根据当前排列计算下一个排列
  20. private void nextPermutation(int[] nums) {
  21. int i = nums.length - 2;
  22. while (i >= 0 && nums[i] >= nums[i + 1]) {
  23. i--;
  24. }
  25. int j = nums.length - 1;
  26. while (nums[j] <= nums[i]) {
  27. j--;
  28. }
  29. swap(nums, i, j);
  30. reverse(nums, i + 1, nums.length - 1);
  31. }
  32. // 判断是否存在下一个排列,即判断是否已经是完全逆序数列
  33. private boolean hasNextPermutation(int[] nums) {
  34. int i = nums.length - 2;
  35. while (i >= 0 && nums[i] >= nums[i + 1]) {
  36. i--;
  37. }
  38. return i >= 0;
  39. }
  40. private void reverse(int[] nums, int left, int right) {
  41. while (left < right) {
  42. swap(nums, left++, right--);
  43. }
  44. }
  45. private void swap(int[] nums, int i, int j) {
  46. int temp = nums[i];
  47. nums[i] = nums[j];
  48. nums[j] = temp;
  49. }
  50. }

JavaScript

回溯法无hash

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permuteUnique = function (nums) {
  let lists = []
  dfs(nums, 0, lists)
  return lists
}


let dfs = function (nums, index, lists) {
  if (index === nums.length) {
    lists.push([...nums])
    return
  }

  let used = new Set()
  for (let i = index; i < nums.length; i++) {
    if (!used.has(nums[i])) {
      used.add(nums[i]);
      [nums[index], nums[i]] = [nums[i], nums[index]]
      dfs(nums, index + 1, lists);
      [nums[index], nums[i]] = [nums[i], nums[index]]
    }
  }
}

回溯法hash

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permuteUnique = function (nums) {
  let lists = []
  dfs(nums, 0, [], lists, new Set())
  return lists
}

let dfs = function (nums, index, list, lists, record) {
  if (index === nums.length) {
    lists.push([...list])
    return
  }

  let used = new Set()
  for (let i = 0; i < nums.length; i++) {
    if (!record.has(i) && !used.has(nums[i])) {
      used.add(nums[i])
      record.add(i)
      list.push(nums[i])
      dfs(nums, index + 1, list, lists, record)
      list.pop()
      record.delete(i)
    }
  }
}