mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 15:36:05 +00:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: permutations_i.cs
|
||||
* Created Time: 2023-04-24
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class permutations_i {
|
||||
/* バックトラッキング:順列 I */
|
||||
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;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
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.RemoveAt(state.Count - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 全順列 I */
|
||||
List<List<int>> PermutationsI(int[] nums) {
|
||||
List<List<int>> res = [];
|
||||
Backtrack([], nums, new bool[nums.Length], res);
|
||||
return res;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [1, 2, 3];
|
||||
|
||||
List<List<int>> res = PermutationsI(nums);
|
||||
|
||||
Console.WriteLine("入力配列 nums = " + string.Join(", ", nums));
|
||||
Console.WriteLine("すべての順列 res = ");
|
||||
foreach (List<int> permutation in res) {
|
||||
PrintUtil.PrintList(permutation);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user