mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-02 18:20:58 +08:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
53
en/codes/java/chapter_backtracking/permutations_ii.java
Normal file
53
en/codes/java/chapter_backtracking/permutations_ii.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user