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:
Yudong Jin
2026-03-30 07:30:15 +08:00
committed by GitHub
parent fe6443235b
commit d7b2277d2b
1444 changed files with 83312 additions and 8363 deletions
@@ -0,0 +1,77 @@
/**
* File: iteration.cs
* Created Time: 2023-08-28
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class iteration {
/* for ループ */
int ForLoop(int n) {
int res = 0;
// 1, 2, ..., n-1, n を順に加算する
for (int i = 1; i <= n; i++) {
res += i;
}
return res;
}
/* while ループ */
int WhileLoop(int n) {
int res = 0;
int i = 1; // 条件変数を初期化する
// 1, 2, ..., n-1, n を順に加算する
while (i <= n) {
res += i;
i += 1; // 条件変数を更新する
}
return res;
}
/* while ループ(2回更新) */
int WhileLoopII(int n) {
int res = 0;
int i = 1; // 条件変数を初期化する
// 1, 4, 10, ... を順に加算する
while (i <= n) {
res += i;
// 条件変数を更新する
i += 1;
i *= 2;
}
return res;
}
/* 二重 for ループ */
string NestedForLoop(int n) {
StringBuilder res = new();
// i = 1, 2, ..., n-1, n とループする
for (int i = 1; i <= n; i++) {
// j = 1, 2, ..., n-1, n とループする
for (int j = 1; j <= n; j++) {
res.Append($"({i}, {j}), ");
}
}
return res.ToString();
}
/* Driver Code */
[Test]
public void Test() {
int n = 5;
int res;
res = ForLoop(n);
Console.WriteLine("\nfor ループの合計結果 res = " + res);
res = WhileLoop(n);
Console.WriteLine("\nwhile ループの合計結果 res = " + res);
res = WhileLoopII(n);
Console.WriteLine("\nwhile ループ(2回更新)の合計結果 res = " + res);
string resStr = NestedForLoop(n);
Console.WriteLine("\n二重 for ループの走査結果 " + resStr);
}
}
@@ -0,0 +1,78 @@
/**
* File: recursion.cs
* Created Time: 2023-08-28
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class recursion {
/* 再帰 */
int Recur(int n) {
// 終了条件
if (n == 1)
return 1;
// 再帰:再帰呼び出し
int res = Recur(n - 1);
// 帰りがけ:結果を返す
return n + res;
}
/* 反復で再帰を模擬する */
int ForLoopRecur(int n) {
// 明示的なスタックを使ってシステムコールスタックを模擬する
Stack<int> stack = new();
int res = 0;
// 再帰:再帰呼び出し
for (int i = n; i > 0; i--) {
// 「スタックへのプッシュ」で「再帰」を模擬する
stack.Push(i);
}
// 帰りがけ:結果を返す
while (stack.Count > 0) {
// 「スタックから取り出す操作」で「帰り」をシミュレート
res += stack.Pop();
}
// res = 1+2+3+...+n
return res;
}
/* 末尾再帰 */
int TailRecur(int n, int res) {
// 終了条件
if (n == 0)
return res;
// 末尾再帰呼び出し
return TailRecur(n - 1, res + n);
}
/* フィボナッチ数列:再帰 */
int Fib(int n) {
// 終了条件 f(1) = 0, f(2) = 1
if (n == 1 || n == 2)
return n - 1;
// f(n) = f(n-1) + f(n-2) を再帰的に呼び出す
int res = Fib(n - 1) + Fib(n - 2);
// 結果 f(n) を返す
return res;
}
/* Driver Code */
[Test]
public void Test() {
int n = 5;
int res;
res = Recur(n);
Console.WriteLine("\n再帰関数の合計結果 res = " + res);
res = ForLoopRecur(n);
Console.WriteLine("\n反復で再帰をシミュレートした合計結果 res = " + res);
res = TailRecur(n, 0);
Console.WriteLine("\n末尾再帰関数の合計結果 res = " + res);
res = Fib(n);
Console.WriteLine("\nフィボナッチ数列の第 " + n + " 項は " + res);
}
}
@@ -0,0 +1,104 @@
/**
* File: space_complexity.cs
* Created Time: 2022-12-23
* Author: haptear (haptear@hotmail.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class space_complexity {
/* 関数 */
int Function() {
// 何らかの処理を行う
return 0;
}
/* 定数階 */
void Constant(int n) {
// 定数、変数、オブジェクトは O(1) の空間を占める
int a = 0;
int b = 0;
int[] nums = new int[10000];
ListNode node = new(0);
// ループ内の変数は O(1) の空間を占める
for (int i = 0; i < n; i++) {
int c = 0;
}
// ループ内の関数は O(1) の空間を占める
for (int i = 0; i < n; i++) {
Function();
}
}
/* 線形階 */
void Linear(int n) {
// 長さ n の配列は O(n) の空間を使用
int[] nums = new int[n];
// 長さ n のリストは O(n) の空間を使用
List<ListNode> nodes = [];
for (int i = 0; i < n; i++) {
nodes.Add(new ListNode(i));
}
// 長さ n のハッシュテーブルは O(n) の空間を使用
Dictionary<int, string> map = [];
for (int i = 0; i < n; i++) {
map.Add(i, i.ToString());
}
}
/* 線形時間(再帰実装) */
void LinearRecur(int n) {
Console.WriteLine("再帰 n = " + n);
if (n == 1) return;
LinearRecur(n - 1);
}
/* 二乗階 */
void Quadratic(int n) {
// 行列は O(n^2) の空間を使用する
int[,] numMatrix = new int[n, n];
// 二次元リストは O(n^2) の空間を使用
List<List<int>> numList = [];
for (int i = 0; i < n; i++) {
List<int> tmp = [];
for (int j = 0; j < n; j++) {
tmp.Add(0);
}
numList.Add(tmp);
}
}
/* 二次時間(再帰実装) */
int QuadraticRecur(int n) {
if (n <= 0) return 0;
int[] nums = new int[n];
Console.WriteLine("再帰 n = " + n + " における nums の長さ = " + nums.Length);
return QuadraticRecur(n - 1);
}
/* 指数時間(完全二分木の構築) */
TreeNode? BuildTree(int n) {
if (n == 0) return null;
TreeNode root = new(0) {
left = BuildTree(n - 1),
right = BuildTree(n - 1)
};
return root;
}
[Test]
public void Test() {
int n = 5;
// 定数階
Constant(n);
// 線形階
Linear(n);
LinearRecur(n);
// 二乗階
Quadratic(n);
QuadraticRecur(n);
// 指数オーダー
TreeNode? root = BuildTree(n);
PrintUtil.PrintTree(root);
}
}
@@ -0,0 +1,195 @@
/**
* File: time_complexity.cs
* Created Time: 2022-12-23
* Author: haptear (haptear@hotmail.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class time_complexity {
void Algorithm(int n) {
int a = 1; // +0(テクニック 1
a += n; // +0(テクニック 1
// +n(テクニック 2
for (int i = 0; i < 5 * n + 1; i++) {
Console.WriteLine(0);
}
// +n*n(テクニック 3
for (int i = 0; i < 2 * n; i++) {
for (int j = 0; j < n + 1; j++) {
Console.WriteLine(0);
}
}
}
// アルゴリズム A の時間計算量: 定数時間
void AlgorithmA(int n) {
Console.WriteLine(0);
}
// アルゴリズム B の時間計算量: 線形時間
void AlgorithmB(int n) {
for (int i = 0; i < n; i++) {
Console.WriteLine(0);
}
}
// アルゴリズム C の時間計算量: 定数時間
void AlgorithmC(int n) {
for (int i = 0; i < 1000000; i++) {
Console.WriteLine(0);
}
}
/* 定数階 */
int Constant(int n) {
int count = 0;
int size = 100000;
for (int i = 0; i < size; i++)
count++;
return count;
}
/* 線形階 */
int Linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
count++;
return count;
}
/* 線形時間(配列を走査) */
int ArrayTraversal(int[] nums) {
int count = 0;
// ループ回数は配列長に比例する
foreach (int num in nums) {
count++;
}
return count;
}
/* 二乗階 */
int Quadratic(int n) {
int count = 0;
// ループ回数はデータサイズ n の二乗に比例する
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
}
}
return count;
}
/* 二次時間(バブルソート) */
int BubbleSort(int[] nums) {
int count = 0; // カウンタ
// 外側のループ:未ソート区間は [0, i]
for (int i = nums.Length - 1; i > 0; i--) {
// 内側のループ:未ソート区間 [0, i] の最大要素をその区間の最右端へ交換
for (int j = 0; j < i; j++) {
if (nums[j] > nums[j + 1]) {
// nums[j] と nums[j + 1] を交換
(nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);
count += 3; // 要素交換には 3 回の単位操作が含まれる
}
}
}
return count;
}
/* 指数時間(ループ実装) */
int Exponential(int n) {
int count = 0, bas = 1;
// 細胞は各ラウンドで 2 つに分裂し、数列 1, 2, 4, 8, ..., 2^(n-1) を形成する
for (int i = 0; i < n; i++) {
for (int j = 0; j < bas; j++) {
count++;
}
bas *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
/* 指数時間(再帰実装) */
int ExpRecur(int n) {
if (n == 1) return 1;
return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;
}
/* 対数時間(ループ実装) */
int Logarithmic(int n) {
int count = 0;
while (n > 1) {
n /= 2;
count++;
}
return count;
}
/* 対数時間(再帰実装) */
int LogRecur(int n) {
if (n <= 1) return 0;
return LogRecur(n / 2) + 1;
}
/* 線形対数時間 */
int LinearLogRecur(int n) {
if (n <= 1) return 1;
int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);
for (int i = 0; i < n; i++) {
count++;
}
return count;
}
/* 階乗時間(再帰実装) */
int FactorialRecur(int n) {
if (n == 0) return 1;
int count = 0;
// 1個から n 個に分裂
for (int i = 0; i < n; i++) {
count += FactorialRecur(n - 1);
}
return count;
}
[Test]
public void Test() {
// n を変えて実行し、各計算量で操作回数がどう変化するかを確認できる
int n = 8;
Console.WriteLine("入力データサイズ n = " + n);
int count = Constant(n);
Console.WriteLine("定数時間の操作回数 = " + count);
count = Linear(n);
Console.WriteLine("線形時間の操作回数 = " + count);
count = ArrayTraversal(new int[n]);
Console.WriteLine("線形時間(配列の走査)の操作回数 = " + count);
count = Quadratic(n);
Console.WriteLine("二乗時間の操作回数 = " + count);
int[] nums = new int[n];
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = BubbleSort(nums);
Console.WriteLine("二乗時間(バブルソート)の操作回数 = " + count);
count = Exponential(n);
Console.WriteLine("指数時間(ループ実装)の操作回数 = " + count);
count = ExpRecur(n);
Console.WriteLine("指数時間(再帰実装)の操作回数 = " + count);
count = Logarithmic(n);
Console.WriteLine("対数時間(ループ実装)の操作回数 = " + count);
count = LogRecur(n);
Console.WriteLine("対数時間(再帰実装)の操作回数 = " + count);
count = LinearLogRecur(n);
Console.WriteLine("線形対数時間(再帰実装)の操作回数 = " + count);
count = FactorialRecur(n);
Console.WriteLine("階乗時間(再帰実装)の操作回数 = " + count);
}
}
@@ -0,0 +1,49 @@
/**
* File: worst_best_time_complexity.cs
* Created Time: 2022-12-23
* Author: haptear (haptear@hotmail.com)
*/
namespace hello_algo.chapter_computational_complexity;
public class worst_best_time_complexity {
/* 要素が { 1, 2, ..., n } で、順序がシャッフルされた配列を生成 */
int[] RandomNumbers(int n) {
int[] nums = new int[n];
// 配列 nums = { 1, 2, 3, ..., n } を生成
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// 配列要素をランダムにシャッフル
for (int i = 0; i < nums.Length; i++) {
int index = new Random().Next(i, nums.Length);
(nums[i], nums[index]) = (nums[index], nums[i]);
}
return nums;
}
/* 配列 nums 内で数値 1 のインデックスを探す */
int FindOne(int[] nums) {
for (int i = 0; i < nums.Length; i++) {
// 要素 1 が配列の先頭にあるとき、最良時間計算量 O(1) となる
// 要素 1 が配列の末尾にあるとき、最悪時間計算量 O(n) となる
if (nums[i] == 1)
return i;
}
return -1;
}
/* Driver Code */
[Test]
public void Test() {
for (int i = 0; i < 10; i++) {
int n = 100;
int[] nums = RandomNumbers(n);
int index = FindOne(nums);
Console.WriteLine("\n配列 [ 1, 2, ..., n ] をシャッフルした後 = " + string.Join(",", nums));
Console.WriteLine("数字 1 のインデックスは " + index);
}
}
}