mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-15 22:57:48 +08:00
Add ru version (#1865)
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
This commit is contained in:
51
ru/codes/java/chapter_backtracking/permutations_i.java
Normal file
51
ru/codes/java/chapter_backtracking/permutations_i.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: permutations_i.java
|
||||
* Created Time: 2023-04-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_backtracking;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class permutations_i {
|
||||
/* Алгоритм бэктрекинга: все перестановки I */
|
||||
public static void backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {
|
||||
// Когда длина состояния равна числу элементов, записать решение
|
||||
if (state.size() == choices.length) {
|
||||
res.add(new ArrayList<Integer>(state));
|
||||
return;
|
||||
}
|
||||
// Перебор всех вариантов выбора
|
||||
for (int i = 0; i < choices.length; i++) {
|
||||
int choice = choices[i];
|
||||
// Отсечение: нельзя выбирать один и тот же элемент повторно
|
||||
if (!selected[i]) {
|
||||
// Попытка: сделать выбор и обновить состояние
|
||||
selected[i] = true;
|
||||
state.add(choice);
|
||||
// Перейти к следующему выбору
|
||||
backtrack(state, choices, selected, res);
|
||||
// Откат: отменить выбор и восстановить предыдущее состояние
|
||||
selected[i] = false;
|
||||
state.remove(state.size() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Все перестановки I */
|
||||
static List<List<Integer>> permutationsI(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, 3 };
|
||||
|
||||
List<List<Integer>> res = permutationsI(nums);
|
||||
|
||||
System.out.println("Входной массив nums = " + Arrays.toString(nums));
|
||||
System.out.println("Все перестановки res = " + res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user