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,5 @@
add_executable(iteration iteration.c)
add_executable(recursion recursion.c)
add_executable(time_complexity time_complexity.c)
add_executable(worst_best_time_complexity worst_best_time_complexity.c)
add_executable(space_complexity space_complexity.c)
@@ -0,0 +1,81 @@
/**
* File: iteration.c
* Created Time: 2023-09-09
* Author: Gonglja (glj0@outlook.com), MwumLi (mwumli@hotmail.com)
*/
#include "../utils/common.h"
/* 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++; // 条件変数を更新する
}
return res;
}
/* while ループ(2回更新) */
int whileLoopII(int n) {
int res = 0;
int i = 1; // 条件変数を初期化する
// 1, 4, 10, ... を順に加算する
while (i <= n) {
res += i;
// 条件変数を更新する
i++;
i *= 2;
}
return res;
}
/* 二重 for ループ */
char *nestedForLoop(int n) {
// n * n は対応する点の個数であり、"(i, j), " に対応する文字列長の最大は 6+10*2 で、さらに末尾の空文字 \0 のための追加領域が必要
int size = n * n * 26 + 1;
char *res = malloc(size * sizeof(char));
// 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++) {
char tmp[26];
snprintf(tmp, sizeof(tmp), "(%d, %d), ", i, j);
strncat(res, tmp, size - strlen(res) - 1);
}
}
return res;
}
/* Driver Code */
int main() {
int n = 5;
int res;
res = forLoop(n);
printf("\nfor ループの合計結果 res = %d\n", res);
res = whileLoop(n);
printf("\nwhile ループの合計結果 res = %d\n", res);
res = whileLoopII(n);
printf("\nwhile ループ(2回更新)の合計結果 res = %d\n", res);
char *resStr = nestedForLoop(n);
printf("\n二重 for ループの走査結果 %s\r\n", resStr);
free(resStr);
return 0;
}
@@ -0,0 +1,77 @@
/**
* File: recursion.c
* Created Time: 2023-09-09
* Author: Gonglja (glj0@outlook.com)
*/
#include "../utils/common.h"
/* 再帰 */
int recur(int n) {
// 終了条件
if (n == 1)
return 1;
// 再帰:再帰呼び出し
int res = recur(n - 1);
// 帰りがけ:結果を返す
return n + res;
}
/* 反復で再帰を模擬する */
int forLoopRecur(int n) {
int stack[1000]; // 大きな配列を使ってスタックを実装する
int top = -1; // スタックトップのインデックス
int res = 0;
// 再帰:再帰呼び出し
for (int i = n; i > 0; i--) {
// 「スタックへのプッシュ」で「再帰」を模擬する
stack[1 + top++] = i;
}
// 帰りがけ:結果を返す
while (top >= 0) {
// 「スタックから取り出す操作」で「帰り」をシミュレート
res += stack[top--];
}
// 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 */
int main() {
int n = 5;
int res;
res = recur(n);
printf("\n再帰関数の合計結果 res = %d\n", res);
res = forLoopRecur(n);
printf("\n反復で再帰をシミュレートした合計結果 res = %d\n", res);
res = tailRecur(n, 0);
printf("\n末尾再帰関数の合計結果 res = %d\n", res);
res = fib(n);
printf("\nフィボナッチ数列の第 %d 項は %d\n", n, res);
return 0;
}
@@ -0,0 +1,141 @@
/**
* File: space_complexity.c
* Created Time: 2023-04-15
* Author: Gonglja (glj0@outlook.com)
*/
#include "../utils/common.h"
/* 関数 */
int func() {
// 何らかの処理を行う
return 0;
}
/* 定数階 */
void constant(int n) {
// 定数、変数、オブジェクトは O(1) の空間を占める
const int a = 0;
int b = 0;
int nums[1000];
ListNode *node = newListNode(0);
free(node);
// ループ内の変数は O(1) の空間を占める
for (int i = 0; i < n; i++) {
int c = 0;
}
// ループ内の関数は O(1) の空間を占める
for (int i = 0; i < n; i++) {
func();
}
}
/* ハッシュテーブル */
typedef struct {
int key;
int val;
UT_hash_handle hh; // uthash.h を用いて実装
} HashTable;
/* 線形階 */
void linear(int n) {
// 長さ n の配列は O(n) の空間を使用
int *nums = malloc(sizeof(int) * n);
free(nums);
// 長さ n のリストは O(n) の空間を使用
ListNode **nodes = malloc(sizeof(ListNode *) * n);
for (int i = 0; i < n; i++) {
nodes[i] = newListNode(i);
}
// メモリを解放する
for (int i = 0; i < n; i++) {
free(nodes[i]);
}
free(nodes);
// 長さ n のハッシュテーブルは O(n) の空間を使用
HashTable *h = NULL;
for (int i = 0; i < n; i++) {
HashTable *tmp = malloc(sizeof(HashTable));
tmp->key = i;
tmp->val = i;
HASH_ADD_INT(h, key, tmp);
}
// メモリを解放する
HashTable *curr, *tmp;
HASH_ITER(hh, h, curr, tmp) {
HASH_DEL(h, curr);
free(curr);
}
}
/* 線形時間(再帰実装) */
void linearRecur(int n) {
printf("再帰 n = %d\r\n", n);
if (n == 1)
return;
linearRecur(n - 1);
}
/* 二乗階 */
void quadratic(int n) {
// 二次元リストは O(n^2) の空間を使用
int **numMatrix = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++) {
int *tmp = malloc(sizeof(int) * n);
for (int j = 0; j < n; j++) {
tmp[j] = 0;
}
numMatrix[i] = tmp;
}
// メモリを解放する
for (int i = 0; i < n; i++) {
free(numMatrix[i]);
}
free(numMatrix);
}
/* 二次時間(再帰実装) */
int quadraticRecur(int n) {
if (n <= 0)
return 0;
int *nums = malloc(sizeof(int) * n);
printf("再帰 n = %d における nums の長さ = %d\r\n", n, n);
int res = quadraticRecur(n - 1);
free(nums);
return res;
}
/* 指数時間(完全二分木の構築) */
TreeNode *buildTree(int n) {
if (n == 0)
return NULL;
TreeNode *root = newTreeNode(0);
root->left = buildTree(n - 1);
root->right = buildTree(n - 1);
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;
}
@@ -0,0 +1,179 @@
/**
* File: time_complexity.c
* Created Time: 2023-01-03
* Author: codingonion (coderonion@gmail.com)
*/
#include "../utils/common.h"
/* 定数階 */
int constant(int n) {
int count = 0;
int size = 100000;
int i = 0;
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 n) {
int count = 0;
// ループ回数は配列長に比例する
for (int i = 0; i < n; i++) {
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 n) {
int count = 0; // カウンタ
// 外側のループ:未ソート区間は [0, i]
for (int i = n - 1; i > 0; i--) {
// 内側のループ:未ソート区間 [0, i] の最大要素をその区間の最右端へ交換
for (int j = 0; j < i; j++) {
if (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 回の単位操作が含まれる
}
}
}
return count;
}
/* 指数時間(ループ実装) */
int exponential(int n) {
int count = 0;
int 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 = 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;
for (int i = 0; i < n; i++) {
count += factorialRecur(n - 1);
}
return count;
}
/* Driver Code */
int main(int argc, char *argv[]) {
// n を変えて実行し、各計算量で操作回数がどう変化するかを確認できる
int n = 8;
printf("入力データサイズ n = %d\n", n);
int count = constant(n);
printf("定数オーダーの操作回数 = %d\n", count);
count = linear(n);
printf("線形オーダーの操作回数 = %d\n", count);
// ヒープ領域にメモリを確保する(要素数 n、要素型 int の一次元可変長配列を作成)
int *nums = (int *)malloc(n * sizeof(int));
count = arrayTraversal(nums, n);
printf("線形オーダー(配列の走査)の操作回数 = %d\n", count);
count = quadratic(n);
printf("平方オーダーの操作回数 = %d\n", count);
for (int i = 0; i < n; i++) {
nums[i] = n - i; // [n,n-1,...,2,1]
}
count = bubbleSort(nums, n);
printf("平方オーダー(バブルソート)の操作回数 = %d\n", count);
count = exponential(n);
printf("指数オーダー(ループ実装)の操作回数 = %d\n", count);
count = expRecur(n);
printf("指数オーダー(再帰実装)の操作回数 = %d\n", count);
count = logarithmic(n);
printf("対数オーダー(ループ実装)の操作回数 = %d\n", count);
count = logRecur(n);
printf("対数オーダー(再帰実装)の操作回数 = %d\n", count);
count = linearLogRecur(n);
printf("線形対数オーダー(再帰実装)の操作回数 = %d\n", count);
count = factorialRecur(n);
printf("階乗オーダー(再帰実装)の操作回数 = %d\n", count);
// ヒープ領域のメモリを解放
if (nums != NULL) {
free(nums);
nums = NULL;
}
getchar();
return 0;
}
@@ -0,0 +1,57 @@
/**
* File: worst_best_time_complexity.c
* Created Time: 2023-01-03
* Author: codingonion (coderonion@gmail.com)
*/
#include "../utils/common.h"
/* 要素が { 1, 2, ..., n } で、順序がシャッフルされた配列を生成 */
int *randomNumbers(int n) {
// ヒープ領域にメモリを確保する(要素数 n、要素型 int の一次元可変長配列を作成)
int *nums = (int *)malloc(n * sizeof(int));
// 配列 nums = { 1, 2, 3, ..., n } を生成
for (int i = 0; i < n; i++) {
nums[i] = i + 1;
}
// 配列要素をランダムにシャッフル
for (int i = n - 1; i > 0; i--) {
int j = rand() % (i + 1);
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
return nums;
}
/* 配列 nums 内で数値 1 のインデックスを探す */
int findOne(int *nums, int n) {
for (int i = 0; i < n; i++) {
// 要素 1 が配列の先頭にあるとき、最良時間計算量 O(1) となる
// 要素 1 が配列の末尾にあるとき、最悪時間計算量 O(n) となる
if (nums[i] == 1)
return i;
}
return -1;
}
/* Driver Code */
int main(int argc, char *argv[]) {
// 乱数シードを初期化する
srand((unsigned int)time(NULL));
for (int i = 0; i < 10; i++) {
int n = 100;
int *nums = randomNumbers(n);
int index = findOne(nums, n);
printf("\n配列 [ 1, 2, ..., n ] をシャッフルした後 = ");
printArray(nums, n);
printf("数値 1 のインデックスは %d\n", index);
// ヒープ領域のメモリを解放
if (nums != NULL) {
free(nums);
nums = NULL;
}
}
return 0;
}