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,59 @@
// File: iteration.go
// Created Time: 2023-08-28
// Author: Reanon (793584285@qq.com)
package chapter_computational_complexity
import "fmt"
/* for ループ */
func forLoop(n int) int {
res := 0
// 1, 2, ..., n-1, n を順に加算する
for i := 1; i <= n; i++ {
res += i
}
return res
}
/* while ループ */
func whileLoop(n int) int {
res := 0
// 条件変数を初期化する
i := 1
// 1, 2, ..., n-1, n を順に加算する
for i <= n {
res += i
// 条件変数を更新する
i++
}
return res
}
/* while ループ(2回更新) */
func whileLoopII(n int) int {
res := 0
// 条件変数を初期化する
i := 1
// 1, 4, 10, ... を順に加算する
for i <= n {
res += i
// 条件変数を更新する
i++
i *= 2
}
return res
}
/* 二重 for ループ */
func nestedForLoop(n int) string {
res := ""
// i = 1, 2, ..., n-1, n とループする
for i := 1; i <= n; i++ {
for j := 1; j <= n; j++ {
// j = 1, 2, ..., n-1, n とループする
res += fmt.Sprintf("(%d, %d), ", i, j)
}
}
return res
}
@@ -0,0 +1,26 @@
// File: iteration_test.go
// Created Time: 2023-08-28
// Author: Reanon (793584285@qq.com)
package chapter_computational_complexity
import (
"fmt"
"testing"
)
/* Driver Code */
func TestIteration(t *testing.T) {
n := 5
res := forLoop(n)
fmt.Println("\nfor ループの合計結果 res = ", res)
res = whileLoop(n)
fmt.Println("\nwhile ループの合計結果 res = ", res)
res = whileLoopII(n)
fmt.Println("\nwhile ループ(2 回更新)の合計結果 res = ", res)
resStr := nestedForLoop(n)
fmt.Println("\n二重 for ループの走査結果 ", resStr)
}
@@ -0,0 +1,61 @@
// File: recursion.go
// Created Time: 2023-08-28
// Author: Reanon (793584285@qq.com)
package chapter_computational_complexity
import "container/list"
/* 再帰 */
func recur(n int) int {
// 終了条件
if n == 1 {
return 1
}
// 再帰:再帰呼び出し
res := recur(n - 1)
// 帰りがけ:結果を返す
return n + res
}
/* 反復で再帰を模擬する */
func forLoopRecur(n int) int {
// 明示的なスタックを使ってシステムコールスタックを模擬する
stack := list.New()
res := 0
// 再帰:再帰呼び出し
for i := n; i > 0; i-- {
// 「スタックへのプッシュ」で「再帰」を模擬する
stack.PushBack(i)
}
// 帰りがけ:結果を返す
for stack.Len() != 0 {
// 「スタックから取り出す操作」で「帰り」をシミュレート
res += stack.Back().Value.(int)
stack.Remove(stack.Back())
}
// res = 1+2+3+...+n
return res
}
/* 末尾再帰 */
func tailRecur(n int, res int) int {
// 終了条件
if n == 0 {
return res
}
// 末尾再帰呼び出し
return tailRecur(n-1, res+n)
}
/* フィボナッチ数列:再帰 */
func fib(n int) int {
// 終了条件 f(1) = 0, f(2) = 1
if n == 1 || n == 2 {
return n - 1
}
// f(n) = f(n-1) + f(n-2) を再帰的に呼び出す
res := fib(n-1) + fib(n-2)
// 結果 f(n) を返す
return res
}
@@ -0,0 +1,26 @@
// File: recursion_test.go
// Created Time: 2023-08-28
// Author: Reanon (793584285@qq.com)
package chapter_computational_complexity
import (
"fmt"
"testing"
)
/* Driver Code */
func TestRecursion(t *testing.T) {
n := 5
res := recur(n)
fmt.Println("\n再帰関数の合計結果 res = ", res)
res = forLoopRecur(n)
fmt.Println("\n反復で再帰をシミュレートした合計結果 res = ", res)
res = tailRecur(n, 0)
fmt.Println("\n末尾再帰関数の合計結果 res = ", res)
res = fib(n)
fmt.Println("\nフィボナッチ数列の第", n, "項は", res)
}
@@ -0,0 +1,106 @@
// File: space_complexity.go
// Created Time: 2022-12-15
// Author: cathay (cathaycchen@gmail.com)
package chapter_computational_complexity
import (
"fmt"
"strconv"
. "github.com/krahets/hello-algo/pkg"
)
/* 構造体 */
type node struct {
val int
next *node
}
/* node 構造体を作成する */
func newNode(val int) *node {
return &node{val: val}
}
/* 関数 */
func function() int {
// いくつかの操作を実行...
return 0
}
/* 定数階 */
func spaceConstant(n int) {
// 定数、変数、オブジェクトは O(1) の空間を占める
const a = 0
b := 0
nums := make([]int, 10000)
node := newNode(0)
// ループ内の変数は O(1) の空間を占める
var c int
for i := 0; i < n; i++ {
c = 0
}
// ループ内の関数は O(1) の空間を占める
for i := 0; i < n; i++ {
function()
}
b += 0
c += 0
nums[0] = 0
node.val = 0
}
/* 線形階 */
func spaceLinear(n int) {
// 長さ n の配列は O(n) の空間を使用
_ = make([]int, n)
// 長さ n のリストは O(n) の空間を使用
var nodes []*node
for i := 0; i < n; i++ {
nodes = append(nodes, newNode(i))
}
// 長さ n のハッシュテーブルは O(n) の空間を使用
m := make(map[int]string, n)
for i := 0; i < n; i++ {
m[i] = strconv.Itoa(i)
}
}
/* 線形時間(再帰実装) */
func spaceLinearRecur(n int) {
fmt.Println("再帰 n =", n)
if n == 1 {
return
}
spaceLinearRecur(n - 1)
}
/* 二乗階 */
func spaceQuadratic(n int) {
// 行列は O(n^2) の空間を使用する
numMatrix := make([][]int, n)
for i := 0; i < n; i++ {
numMatrix[i] = make([]int, n)
}
}
/* 二次時間(再帰実装) */
func spaceQuadraticRecur(n int) int {
if n <= 0 {
return 0
}
nums := make([]int, n)
fmt.Printf("再帰 n = %d における nums の長さ = %d \n", n, len(nums))
return spaceQuadraticRecur(n - 1)
}
/* 指数時間(完全二分木の構築) */
func buildTree(n int) *TreeNode {
if n == 0 {
return nil
}
root := NewTreeNode(0)
root.Left = buildTree(n - 1)
root.Right = buildTree(n - 1)
return root
}
@@ -0,0 +1,26 @@
// File: space_complexity_test.go
// Created Time: 2022-12-15
// Author: cathay (cathaycchen@gmail.com)
package chapter_computational_complexity
import (
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestSpaceComplexity(t *testing.T) {
n := 5
// 定数階
spaceConstant(n)
// 線形階
spaceLinear(n)
spaceLinearRecur(n)
// 二乗階
spaceQuadratic(n)
spaceQuadraticRecur(n)
// 指数オーダー
root := buildTree(n)
PrintTree(root)
}
@@ -0,0 +1,130 @@
// File: time_complexity.go
// Created Time: 2022-12-13
// Author: msk397 (machangxinq@gmail.com)
package chapter_computational_complexity
/* 定数階 */
func constant(n int) int {
count := 0
size := 100000
for i := 0; i < size; i++ {
count++
}
return count
}
/* 線形階 */
func linear(n int) int {
count := 0
for i := 0; i < n; i++ {
count++
}
return count
}
/* 線形時間(配列を走査) */
func arrayTraversal(nums []int) int {
count := 0
// ループ回数は配列長に比例する
for range nums {
count++
}
return count
}
/* 二乗階 */
func quadratic(n int) int {
count := 0
// ループ回数はデータサイズ n の二乗に比例する
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
count++
}
}
return count
}
/* 二次時間(バブルソート) */
func bubbleSort(nums []int) int {
count := 0 // カウンタ
// 外側のループ:未ソート区間は [0, i]
for i := len(nums) - 1; i > 0; i-- {
// 内側のループ:未ソート区間 [0, i] の最大要素をその区間の最右端へ交換
for j := 0; j < i; j++ {
if nums[j] > nums[j+1] {
// nums[j] と nums[j + 1] を交換
tmp := nums[j]
nums[j] = nums[j+1]
nums[j+1] = tmp
count += 3 // 要素交換には 3 回の単位操作が含まれる
}
}
}
return count
}
/* 指数時間(ループ実装) */
func exponential(n int) int {
count, base := 0, 1
// 細胞は各ラウンドで 2 つに分裂し、数列 1, 2, 4, 8, ..., 2^(n-1) を形成する
for i := 0; i < n; i++ {
for j := 0; j < base; j++ {
count++
}
base *= 2
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count
}
/* 指数時間(再帰実装) */
func expRecur(n int) int {
if n == 1 {
return 1
}
return expRecur(n-1) + expRecur(n-1) + 1
}
/* 対数時間(ループ実装) */
func logarithmic(n int) int {
count := 0
for n > 1 {
n = n / 2
count++
}
return count
}
/* 対数時間(再帰実装) */
func logRecur(n int) int {
if n <= 1 {
return 0
}
return logRecur(n/2) + 1
}
/* 線形対数時間 */
func linearLogRecur(n int) int {
if n <= 1 {
return 1
}
count := linearLogRecur(n/2) + linearLogRecur(n/2)
for i := 0; i < n; i++ {
count++
}
return count
}
/* 階乗時間(再帰実装) */
func factorialRecur(n int) int {
if n == 0 {
return 1
}
count := 0
// 1個から n 個に分裂
for i := 0; i < n; i++ {
count += factorialRecur(n - 1)
}
return count
}
@@ -0,0 +1,48 @@
// File: time_complexity_test.go
// Created Time: 2022-12-13
// Author: msk397 (machangxinq@gmail.com)
package chapter_computational_complexity
import (
"fmt"
"testing"
)
func TestTimeComplexity(t *testing.T) {
n := 8
fmt.Println("入力データサイズ n =", n)
count := constant(n)
fmt.Println("定数時間の操作回数 =", count)
count = linear(n)
fmt.Println("線形時間の操作回数 =", count)
count = arrayTraversal(make([]int, n))
fmt.Println("線形時間(配列走査)の操作回数 =", count)
count = quadratic(n)
fmt.Println("二乗時間の操作回数 =", count)
nums := make([]int, n)
for i := 0; i < n; i++ {
nums[i] = n - i
}
count = bubbleSort(nums)
fmt.Println("二乗時間(バブルソート)の操作回数 =", count)
count = exponential(n)
fmt.Println("指数時間(ループ実装)の操作回数 =", count)
count = expRecur(n)
fmt.Println("指数時間(再帰実装)の操作回数 =", count)
count = logarithmic(n)
fmt.Println("対数時間(ループ実装)の操作回数 =", count)
count = logRecur(n)
fmt.Println("対数時間(再帰実装)の操作回数 =", count)
count = linearLogRecur(n)
fmt.Println("線形対数時間(再帰実装)の操作回数 =", count)
count = factorialRecur(n)
fmt.Println("階乗時間(再帰実装)の操作回数 =", count)
}
@@ -0,0 +1,35 @@
// File: worst_best_time_complexity.go
// Created Time: 2022-12-13
// Author: msk397 (machangxinq@gmail.com), cathay (cathaycchen@gmail.com)
package chapter_computational_complexity
import (
"math/rand"
)
/* 要素が { 1, 2, ..., n } で、順序がシャッフルされた配列を生成 */
func randomNumbers(n int) []int {
nums := make([]int, n)
// 配列 nums = { 1, 2, 3, ..., n } を生成
for i := 0; i < n; i++ {
nums[i] = i + 1
}
// 配列要素をランダムにシャッフル
rand.Shuffle(len(nums), func(i, j int) {
nums[i], nums[j] = nums[j], nums[i]
})
return nums
}
/* 配列 nums 内で数値 1 のインデックスを探す */
func findOne(nums []int) int {
for i := 0; i < len(nums); i++ {
// 要素 1 が配列の先頭にあるとき、最良時間計算量 O(1) となる
// 要素 1 が配列の末尾にあるとき、最悪時間計算量 O(n) となる
if nums[i] == 1 {
return i
}
}
return -1
}
@@ -0,0 +1,20 @@
// File: worst_best_time_complexity_test.go
// Created Time: 2022-12-13
// Author: msk397 (machangxinq@gmail.com), cathay (cathaycchen@gmail.com)
package chapter_computational_complexity
import (
"fmt"
"testing"
)
func TestWorstBestTimeComplexity(t *testing.T) {
for i := 0; i < 10; i++ {
n := 100
nums := randomNumbers(n)
index := findOne(nums)
fmt.Println("\n配列 [ 1, 2, ..., n ] をシャッフルした後 =", nums)
fmt.Println("数字 1 のインデックスは", index)
}
}