mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-01 18:55:23 +08:00
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
54 lines
2.0 KiB
Java
54 lines
2.0 KiB
Java
/**
|
|
* File: permutations_ii.java
|
|
* Created Time: 2023-04-24
|
|
* Author: krahets ([email protected])
|
|
*/
|
|
|
|
package chapter_backtracking;
|
|
|
|
import java.util.*;
|
|
|
|
public class permutations_ii {
|
|
/* 백트래킹 알고리즘: 모든 순열 II */
|
|
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;
|
|
}
|
|
// 모든 선택지를 순회한다
|
|
Set<Integer> duplicated = new HashSet<Integer>();
|
|
for (int i = 0; i < choices.length; i++) {
|
|
int choice = choices[i];
|
|
// 가지치기: 원소를 중복하여 선택할 수 없으며, 값이 같은 원소도 중복하여 선택할 수 없다
|
|
if (!selected[i] && !duplicated.contains(choice)) {
|
|
// 시도: 선택하고 상태를 갱신한다
|
|
duplicated.add(choice); // 선택한 원소의 값을 기록한다
|
|
selected[i] = true;
|
|
state.add(choice);
|
|
// 다음 선택을 진행한다
|
|
backtrack(state, choices, selected, res);
|
|
// 되돌리기: 선택을 취소하고 이전 상태로 되돌린다
|
|
selected[i] = false;
|
|
state.remove(state.size() - 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* 모든 순열 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("입력 배열 nums = " + Arrays.toString(nums));
|
|
System.out.println("모든 순열 res = " + res);
|
|
}
|
|
}
|