47.Permutations-II

47. Permutations II

้ข˜็›ฎๅœฐๅ€

https://leetcode.com/problems/permutations-ii/

https://www.lintcode.com/problem/permutations-ii/description

้ข˜็›ฎๆ่ฟฐ

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

Example:

Input: [1,1,2]
Output:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]

ไปฃ็ 

public class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
    List<List<Integer>> results = new ArrayList<>();
    if (nums == null) return results;

    Arrays.sort(nums);

    boolean[] visited = new boolean[nums.length];
    List<Integer> permutation = new ArrayList<Integer>();
    dfs(nums, visited, permutation, results);

    return results;
  }

  private void dfs(int[] nums, boolean[] visited, List<Integer> permutation, List<List<Integer>> results) {
    if (nums.length == permutation.size()) {
      results.add(new ArrayList<Integer>(permutation));
      return;
    }

    for (int i = 0; i < nums.length; i++) {
      if (visited[i]) continue;

      // skipping causes duplicate situations
      if (i > 0 && nums[i] == nums[i - 1] && visited[i - 1] == false) {
        // ๅ‡่ฎพ่พ“ๅ…ฅ็š„ๆ•ฐ็ป„ไธบ[1๏ผŒ1๏ผŒ2]ใ€‚ๅˆ™ๅฝ“็ฌฌไธ€ไธช1่ขซๆทปๅŠ ่ฟ›็ป“ๆžœ้›†ๆ—ถ๏ผŒๅฏไปฅ็ปง็ปญไฝฟ็”จ็ฌฌไบŒไธช1ไฝœไธบๅ…ƒ็ด ๆทปๅŠ ่ฟ›็ป“ๆžœ้›†ไปŽ่€Œ็”Ÿๆˆ112ใ€‚ๅŒ็†๏ผŒๅฝ“่ฏ•ๅ›พๅฐ†็ฌฌไบŒไธช1ไฝœไธบ็ฌฌไธ€ไธชๅ…ƒ็ด ๆทปๅŠ ่ฟ›็ป“ๆžœ้›†ๆ—ถ๏ผŒๅช่ฆ็ฌฌไธ€ไธช1่ฟ˜ๆฒกๆœ‰่ขซไฝฟ็”จ่ฟ‡๏ผŒๅˆ™ไธๅฏไปฅไฝฟ็”จ็ฌฌไบŒไธช1ใ€‚ๅ› ๆญค๏ผŒ112ไธไผš่ขซ้‡ๅค็š„ๆทปๅŠ ่ฟ›็ป“ๆžœ้›†ใ€‚
        continue;
      }

      permutation.add(nums[i]);
      visited[i] = true;
      dfs(nums, visited, permutation, results);
      visited[i] = false;
      permuation.remove(permutation.size() - 1);
    }

  }
}

Approach 2: ๅญ—ๅ…ธๅบ

class Solution {
  public ArrayList<ArrayList<Integer>> permuteUnique(ArrayList<Integer> nums) {
      ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
      if (nums == null || nums.size() == 0) return result;

      // deep copy
      List<Integer> perm = new ArrayList<Integer>(nums);
      Collections.sort(perm);

      while (true) {
        result.add(new ArrayList<Integer>(perm));
        // step1: search the first num[k] < nums[k+1] backward
        int k = -1;
        for (int i = perm.size() - 2; i >= 0; i--) ใ€
          if (perm.get(i) < perm.get(i + 1)) {
            k = i;
            break;
          }
      }
      // if current rank is the largest, exit while loop
      if (k == -1) break;

      // step2: search teh first perm[k] < perm[l] backward
      int l = perm.size() - 1;
      while (l > k && perm.get(l) <= perm.get(k)) l--;

      // step3: swap perm[k] with perm[l]
      Collections.swap(perm, k, l);

      // step4: reverse between k+1 and perm.length - 1
      reverse(perm, k+1, perm.size() - 1);
    }

        return result;
    }

    private void reverse(List<Integer> nums, int left, int right) {
      for (int l = left, r = right; l < r; l++, r--) {
        Collections.swap(nums, l, r);
      }
  }
}

Last updated