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

View File

@@ -0,0 +1,5 @@
add_executable(iteration iteration.cpp)
add_executable(recursion recursion.cpp)
add_executable(space_complexity space_complexity.cpp)
add_executable(time_complexity time_complexity.cpp)
add_executable(worst_best_time_complexity worst_best_time_complexity.cpp)

View File

@@ -9,7 +9,7 @@
/* for ループ */
int forLoop(int n) {
int res = 0;
// 1, 2, ..., n-1, n の合計をループ計算
// 1, 2, ..., n-1, n を順に加算する
for (int i = 1; i <= n; ++i) {
res += i;
}
@@ -19,35 +19,35 @@ int forLoop(int n) {
/* while ループ */
int whileLoop(int n) {
int res = 0;
int i = 1; // 条件変数を初期化
// 1, 2, ..., n-1, n の合計をループ計算
int i = 1; // 条件変数を初期化する
// 1, 2, ..., n-1, n を順に加算する
while (i <= n) {
res += i;
i++; // 条件変数を更新
i++; // 条件変数を更新する
}
return res;
}
/* while ループ2つの更新) */
/* while ループ2更新) */
int whileLoopII(int n) {
int res = 0;
int i = 1; // 条件変数を初期化
// 1, 4, 10, ... の合計をループ計算
int i = 1; // 条件変数を初期化する
// 1, 4, 10, ... を順に加算する
while (i <= n) {
res += i;
// 条件変数を更新
// 条件変数を更新する
i++;
i *= 2;
}
return res;
}
/* 2重 for ループ */
/* 重 for ループ */
string nestedForLoop(int n) {
ostringstream res;
// ループ i = 1, 2, ..., n-1, n
// i = 1, 2, ..., n-1, n とループする
for (int i = 1; i <= n; ++i) {
// ループ j = 1, 2, ..., n-1, n
// j = 1, 2, ..., n-1, n とループする
for (int j = 1; j <= n; ++j) {
res << "(" << i << ", " << j << "), ";
}
@@ -55,7 +55,7 @@ string nestedForLoop(int n) {
return res.str();
}
/* ドライバーコード */
/* Driver Code */
int main() {
int n = 5;
int res;
@@ -67,10 +67,10 @@ int main() {
cout << "\nwhile ループの合計結果 res = " << res << endl;
res = whileLoopII(n);
cout << "\nwhile ループ2つの更新)の合計結果 res = " << res << endl;
cout << "\nwhile ループ2更新)の合計結果 res = " << res << endl;
string resStr = nestedForLoop(n);
cout << "\n2重 for ループ走査結果 = " << resStr << endl;
cout << "\n重 for ループ走査結果 " << resStr << endl;
return 0;
}
}

View File

@@ -13,23 +13,23 @@ int recur(int n) {
return 1;
// 再帰:再帰呼び出し
int res = recur(n - 1);
// 戻り値:結果を返す
// 帰りがけ:結果を返す
return n + res;
}
/* 反復で再帰をシミュレート */
/* 反復で再帰を模擬する */
int forLoopRecur(int n) {
// 明示的なスタックを使用してシステムコールスタックをシミュレート
// 明示的なスタックを使てシステムコールスタックを模擬する
stack<int> stack;
int res = 0;
// 再帰:再帰呼び出し
for (int i = n; i > 0; i--) {
// 「スタックへのプッシュ」で「再帰」をシミュレート
// 「スタックへのプッシュ」で「再帰」を模擬する
stack.push(i);
}
// 戻り値:結果を返す
// 帰りがけ:結果を返す
while (!stack.empty()) {
// 「スタックからのポップ」で「戻り値」をシミュレート
// 「スタックから取り出す操作」で「帰り」をシミュレート
res += stack.top();
stack.pop();
}
@@ -51,13 +51,13 @@ 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)
// f(n) = f(n-1) + f(n-2) を再帰的に呼び出す
int res = fib(n - 1) + fib(n - 2);
// 結果 f(n) を返す
return res;
}
/* ドライバーコード */
/* Driver Code */
int main() {
int n = 5;
int res;
@@ -66,13 +66,13 @@ int main() {
cout << "\n再帰関数の合計結果 res = " << res << endl;
res = forLoopRecur(n);
cout << "\n反復を使用して再帰をシミュレートした合計結果 res = " << res << endl;
cout << "\n反復再帰をシミュレートした合計結果 res = " << res << endl;
res = tailRecur(n, 0);
cout << "\n末尾再帰関数の合計結果 res = " << res << endl;
res = fib(n);
cout << "フィボナッチ数列の第 " << n << " 番目の数" << res << endl;
cout << "\nフィボナッチ数列の第 " << n << " " << res << endl;
return 0;
}
}

View File

@@ -8,44 +8,44 @@
/* 関数 */
int func() {
// 何らかの操作を実行
// 何らかの処理を行う
return 0;
}
/* 定数計算量 */
/* 定数 */
void constant(int n) {
// 定数、変数、オブジェクトは O(1) 空間を占める
// 定数、変数、オブジェクトは O(1) 空間を占める
const int a = 0;
int b = 0;
vector<int> nums(10000);
ListNode node(0);
// ループ内の変数は O(1) 空間を占める
// ループ内の変数は O(1) 空間を占める
for (int i = 0; i < n; i++) {
int c = 0;
}
// ループ内の関数は O(1) 空間を占める
// ループ内の関数は O(1) 空間を占める
for (int i = 0; i < n; i++) {
func();
}
}
/* 線形計算量 */
/* 線形 */
void linear(int n) {
// 長さ n の配列は O(n) 空間を占める
// 長さ n の配列は O(n) 空間を使用
vector<int> nums(n);
// 長さ n のリストは O(n) 空間を占める
// 長さ n のリストは O(n) 空間を使用
vector<ListNode> nodes;
for (int i = 0; i < n; i++) {
nodes.push_back(ListNode(i));
}
// 長さ n のハッシュテーブルは O(n) 空間を占める
// 長さ n のハッシュテーブルは O(n) 空間を使用
unordered_map<int, string> map;
for (int i = 0; i < n; i++) {
map[i] = to_string(i);
}
}
/* 線形計算量(再帰実装) */
/* 線形時間(再帰実装) */
void linearRecur(int n) {
cout << "再帰 n = " << n << endl;
if (n == 1)
@@ -53,9 +53,9 @@ void linearRecur(int n) {
linearRecur(n - 1);
}
/* 二次計算量 */
/* 二乗階 */
void quadratic(int n) {
// 二次元リストは O(n^2) 空間を占める
// 二次元リストは O(n^2) 空間を使用
vector<vector<int>> numMatrix;
for (int i = 0; i < n; i++) {
vector<int> tmp;
@@ -66,16 +66,16 @@ void quadratic(int n) {
}
}
/* 二次計算量(再帰実装) */
/* 二次時間(再帰実装) */
int quadraticRecur(int n) {
if (n <= 0)
return 0;
vector<int> nums(n);
cout << "再帰 n = " << n << ", nums の長さ = " << nums.size() << endl;
cout << "再帰 n = " << n << " における nums の長さ = " << nums.size() << endl;
return quadraticRecur(n - 1);
}
/* 指数計算量(完全二分木の構築) */
/* 指数時間(完全二分木の構築) */
TreeNode *buildTree(int n) {
if (n == 0)
return nullptr;
@@ -85,23 +85,23 @@ TreeNode *buildTree(int n) {
return root;
}
/* ドライバーコード */
/* Driver Code */
int main() {
int n = 5;
// 定数計算量
// 定数
constant(n);
// 線形計算量
// 線形
linear(n);
linearRecur(n);
// 二次計算量
// 二乗階
quadratic(n);
quadraticRecur(n);
// 指数計算量
// 指数オーダー
TreeNode *root = buildTree(n);
printTree(root);
// メモリを解放
// メモリを解放する
freeMemoryTree(root);
return 0;
}
}

View File

@@ -6,7 +6,7 @@
#include "../utils/common.hpp"
/* 定数計算量 */
/* 定数 */
int constant(int n) {
int count = 0;
int size = 100000;
@@ -15,7 +15,7 @@ int constant(int n) {
return count;
}
/* 線形計算量 */
/* 線形 */
int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++)
@@ -23,20 +23,20 @@ int linear(int n) {
return count;
}
/* 線形計算量(配列走査) */
/* 線形時間(配列走査) */
int arrayTraversal(vector<int> &nums) {
int count = 0;
// ループ回数は配列の長さに比例
// ループ回数は配列に比例する
for (int num : nums) {
count++;
}
return count;
}
/* 二次計算量 */
/* 二乗階 */
int quadratic(int n) {
int count = 0;
// ループ回数はデータサイズ n の二乗に比例
// ループ回数はデータサイズ n の二乗に比例する
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count++;
@@ -45,29 +45,29 @@ int quadratic(int n) {
return count;
}
/* 二次計算量(バブルソート) */
/* 二次時間(バブルソート) */
int bubbleSort(vector<int> &nums) {
int count = 0; // カウンタ
// 外側ループ:未ソート範囲は [0, i]
int count = 0; // カウンタ
// 外側ループ:未ソート区間は [0, i]
for (int i = nums.size() - 1; i > 0; 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] と nums[j + 1] を交換
int tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // 要素のスワップには3つの個別操作が含まれる
count += 3; // 要素交換には 3 回の単位操作が含まれる
}
}
}
return count;
}
/* 指数計算量(ループ実装) */
/* 指数時間(ループ実装) */
int exponential(int n) {
int count = 0, base = 1;
// セルは毎ラウンド2つに分裂し、数列 1, 2, 4, 8, ..., 2^(n-1) を形成
// 細胞は各ラウンドで 2 つに分裂し、数列 1, 2, 4, 8, ..., 2^(n-1) を形成する
for (int i = 0; i < n; i++) {
for (int j = 0; j < base; j++) {
count++;
@@ -78,14 +78,14 @@ int exponential(int n) {
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) {
@@ -95,14 +95,14 @@ int logarithmic(int n) {
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;
@@ -113,56 +113,56 @@ int linearLogRecur(int n) {
return count;
}
/* 階乗計算量(再帰実装) */
/* 階乗時間(再帰実装) */
int factorialRecur(int n) {
if (n == 0)
return 1;
int count = 0;
// 1から n に分裂
// 1から n に分裂
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
/* ドライバーコード */
/* Driver Code */
int main() {
// n を変更して、さまざまな計算量で操作回数の変化傾向を体験可能
// n を変えて実行し、各計算量で操作回数がどう変化するかを確認できる
int n = 8;
cout << "入力データサイズ n = " << n << endl;
int count = constant(n);
cout << "定数計算量の操作回数 = " << count << endl;
cout << "定数オーダーの操作回数 = " << count << endl;
count = linear(n);
cout << "線形計算量の操作回数 = " << count << endl;
cout << "線形オーダーの操作回数 = " << count << endl;
vector<int> arr(n);
count = arrayTraversal(arr);
cout << "線形計算量の操作回数(配列走査) = " << count << endl;
cout << "線形オーダー(配列走査)の操作回数 = " << count << endl;
count = quadratic(n);
cout << "次計算量の操作回数 = " << count << endl;
cout << "乗オーダーの操作回数 = " << count << endl;
vector<int> nums(n);
for (int i = 0; i < n; i++)
nums[i] = n - i; // [n,n-1,...,2,1]
count = bubbleSort(nums);
cout << "次計算量の操作回数(バブルソート) = " << count << endl;
cout << "乗オーダー(バブルソート)の操作回数 = " << count << endl;
count = exponential(n);
cout << "指数計算量の操作回数(ループ実装) = " << count << endl;
cout << "指数オーダー(ループ実装)の操作回数 = " << count << endl;
count = expRecur(n);
cout << "指数計算量の操作回数(再帰実装) = " << count << endl;
cout << "指数オーダー(再帰実装)の操作回数 = " << count << endl;
count = logarithmic(n);
cout << "対数計算量の操作回数(ループ実装) = " << count << endl;
cout << "対数オーダー(ループ実装)の操作回数 = " << count << endl;
count = logRecur(n);
cout << "対数計算量の操作回数(再帰実装) = " << count << endl;
cout << "対数オーダー(再帰実装)の操作回数 = " << count << endl;
count = linearLogRecur(n);
cout << "線形対数計算量の操作回数(再帰実装) = " << count << endl;
cout << "線形対数オーダー(再帰実装)の操作回数 = " << count << endl;
count = factorialRecur(n);
cout << "階乗計算量の操作回数(再帰実装) = " << count << endl;
cout << "階乗オーダー(再帰実装)の操作回数 = " << count << endl;
return 0;
}
}

View File

@@ -6,40 +6,40 @@
#include "../utils/common.hpp"
/* 要素 {1, 2, ..., n} をランダムにシャッフルた配列を生成 */
/* 要素 { 1, 2, ..., n } で、順序がシャッフルされた配列を生成 */
vector<int> randomNumbers(int n) {
vector<int> nums(n);
// 配列 nums = { 1, 2, 3, ..., n } を生成
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// システム時刻を使用してランダムシードを生成
// システム時刻を使って乱数シードを生成する
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
// 配列要素をランダムにシャッフル
shuffle(nums.begin(), nums.end(), default_random_engine(seed));
return nums;
}
/* 配列 nums で数値1のインデックスを見つける */
/* 配列 nums で数値 1 のインデックスを探す */
int findOne(vector<int> &nums) {
for (int i = 0; i < nums.size(); i++) {
// 要素1が配列の先頭にある場合、最良時間計算量 O(1) を達成
// 要素1が配列の末尾にある場合、最悪時間計算量 O(n) を達成
// 要素 1 が配列の先頭にあるとき、最良時間計算量 O(1) となる
// 要素 1 が配列の末尾にあるとき、最悪時間計算量 O(n) となる
if (nums[i] == 1)
return i;
}
return -1;
}
/* ドライバーコード */
/* Driver Code */
int main() {
for (int i = 0; i < 1000; i++) {
int n = 100;
vector<int> nums = randomNumbers(n);
int index = findOne(nums);
cout << "\n配列 [ 1, 2, ..., n ] をシャッフル後 = ";
cout << "\n配列 [ 1, 2, ..., n ] をシャッフルした後 = ";
printVector(nums);
cout << "値1のインデックスは " << index << endl;
cout << "字 1 のインデックスは " << index << endl;
}
return 0;
}
}