/** * 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 state, int[] choices, boolean[] selected, List> res) { // When the state length equals the number of elements, record the solution if (state.size() == choices.length) { res.add(new ArrayList(state)); return; } // Traverse all choices Set duplicated = new HashSet(); 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> permutationsII(int[] nums) { List> res = new ArrayList>(); backtrack(new ArrayList(), nums, new boolean[nums.length], res); return res; } public static void main(String[] args) { int[] nums = { 1, 2, 2 }; List> res = permutationsII(nums); System.out.println("Input array nums = " + Arrays.toString(nums)); System.out.println("All permutations res = " + res); } }