You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.9 KiB
54 lines
1.9 KiB
/**
|
|
* File: permutations_ii.java
|
|
* Created Time: 2023-04-24
|
|
* Author: krahets (krahets@163.com)
|
|
*/
|
|
|
|
package chapter_backtracking;
|
|
|
|
import java.util.*;
|
|
|
|
public class permutations_ii {
|
|
/* Backtracking algorithm: Permutation II */
|
|
static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
|
|
// When the state length equals the number of elements, record the solution
|
|
if (state.size() == choices.length) {
|
|
res.add(new ArrayList<Integer>(state));
|
|
return;
|
|
}
|
|
// Traverse all choices
|
|
Set<Integer> duplicated = new HashSet<Integer>();
|
|
for (int i = 0; i < choices.length; i++) {
|
|
int choice = choices[i];
|
|
// Pruning: do not allow repeated selection of elements and do not allow repeated selection of equal elements
|
|
if (!selected[i] && !duplicated.contains(choice)) {
|
|
// Attempt: make a choice, update the state
|
|
duplicated.add(choice); // Record selected element values
|
|
selected[i] = true;
|
|
state.add(choice);
|
|
// Proceed to the next round of selection
|
|
backtrack(state, choices, selected, res);
|
|
// Retract: undo the choice, restore to the previous state
|
|
selected[i] = false;
|
|
state.remove(state.size() - 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Permutation II */
|
|
static List<List<Integer>> permutationsII(int[] nums) {
|
|
List<List<Integer>> res = new ArrayList<List<Integer>>();
|
|
backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);
|
|
return res;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int[] nums = { 1, 2, 2 };
|
|
|
|
List<List<Integer>> res = permutationsII(nums);
|
|
|
|
System.out.println("Input array nums = " + Arrays.toString(nums));
|
|
System.out.println("All permutations res = " + res);
|
|
}
|
|
}
|