mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-15 22:57:48 +08:00
56 lines
1.9 KiB
C#
56 lines
1.9 KiB
C#
/**
|
|
* File: permutations_ii.cs
|
|
* Created Time: 2023-04-24
|
|
* Author: hpstory (hpstory1024@163.com)
|
|
*/
|
|
|
|
namespace hello_algo.chapter_backtracking;
|
|
|
|
public class permutations_ii {
|
|
/* バックトラッキング:順列 II */
|
|
void Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {
|
|
// 状態の長さが要素数に等しければ、解を記録
|
|
if (state.Count == choices.Length) {
|
|
res.Add(new List<int>(state));
|
|
return;
|
|
}
|
|
// すべての選択肢を走査
|
|
HashSet<int> duplicated = [];
|
|
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.RemoveAt(state.Count - 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* 全順列 II */
|
|
List<List<int>> PermutationsII(int[] nums) {
|
|
List<List<int>> res = [];
|
|
Backtrack([], nums, new bool[nums.Length], res);
|
|
return res;
|
|
}
|
|
|
|
[Test]
|
|
public void Test() {
|
|
int[] nums = [1, 2, 2];
|
|
|
|
List<List<int>> res = PermutationsII(nums);
|
|
|
|
Console.WriteLine("入力配列 nums = " + string.Join(", ", nums));
|
|
Console.WriteLine("すべての順列 res = ");
|
|
foreach (List<int> permutation in res) {
|
|
PrintUtil.PrintList(permutation);
|
|
}
|
|
}
|
|
}
|