mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-22 11:26:07 +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,34 @@
|
||||
// File: binary_search_recur.go
|
||||
// Created Time: 2023-07-19
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
/* 二分探索:問題 f(i, j) */
|
||||
func dfs(nums []int, target, i, j int) int {
|
||||
// 区間が空なら対象要素は存在しないため、-1 を返す
|
||||
if i > j {
|
||||
return -1
|
||||
}
|
||||
// 中点インデックスを計算する
|
||||
m := i + ((j - i) >> 1)
|
||||
// 中点の要素と目標要素の大小を判定する
|
||||
if nums[m] < target {
|
||||
// 小さければ右半分の配列を再帰
|
||||
// 部分問題 f(m+1, j) を解く
|
||||
return dfs(nums, target, m+1, j)
|
||||
} else if nums[m] > target {
|
||||
// 大きければ左半分の配列を再帰
|
||||
// 部分問題 f(i, m-1) を解く
|
||||
return dfs(nums, target, i, m-1)
|
||||
} else {
|
||||
// 目標要素が見つかったらそのインデックスを返す
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
/* 二分探索 */
|
||||
func binarySearch(nums []int, target int) int {
|
||||
n := len(nums)
|
||||
return dfs(nums, target, 0, n-1)
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// File: binary_search_recur_test.go
|
||||
// Created Time: 2023-07-19
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBinarySearch(t *testing.T) {
|
||||
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
||||
target := 6
|
||||
noTarget := 99
|
||||
targetIndex := binarySearch(nums, target)
|
||||
fmt.Println("対象要素 6 のインデックス = ", targetIndex)
|
||||
noTargetIndex := binarySearch(nums, noTarget)
|
||||
fmt.Println("対象要素が存在しないときのインデックス = ", noTargetIndex)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// File: build_tree.go
|
||||
// Created Time: 2023-07-20
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import . "github.com/krahets/hello-algo/pkg"
|
||||
|
||||
/* 二分木を構築:分割統治 */
|
||||
func dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {
|
||||
// 部分木区間が空なら終了する
|
||||
if r-l < 0 {
|
||||
return nil
|
||||
}
|
||||
// ルートノードを初期化する
|
||||
root := NewTreeNode(preorder[i])
|
||||
// m を求めて左右部分木を分割する
|
||||
m := inorderMap[preorder[i]]
|
||||
// 部分問題:左部分木を構築する
|
||||
root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)
|
||||
// 部分問題:右部分木を構築する
|
||||
root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)
|
||||
// 根ノードを返す
|
||||
return root
|
||||
}
|
||||
|
||||
/* 二分木を構築 */
|
||||
func buildTree(preorder, inorder []int) *TreeNode {
|
||||
// inorder の要素からインデックスへの対応を格納するハッシュテーブルを初期化する
|
||||
inorderMap := make(map[int]int, len(inorder))
|
||||
for i := 0; i < len(inorder); i++ {
|
||||
inorderMap[inorder[i]] = i
|
||||
}
|
||||
|
||||
root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)
|
||||
return root
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// File: build_tree_test.go
|
||||
// Created Time: 2023-07-20
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestBuildTree(t *testing.T) {
|
||||
preorder := []int{3, 9, 2, 1, 7}
|
||||
inorder := []int{9, 3, 1, 2, 7}
|
||||
fmt.Print("前順走査 = ")
|
||||
PrintSlice(preorder)
|
||||
fmt.Print("中順走査 = ")
|
||||
PrintSlice(inorder)
|
||||
|
||||
root := buildTree(preorder, inorder)
|
||||
fmt.Println("構築した二分木は:")
|
||||
PrintTree(root)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// File: hanota.go
|
||||
// Created Time: 2023-07-21
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import "container/list"
|
||||
|
||||
/* 円盤を 1 枚移動 */
|
||||
func move(src, tar *list.List) {
|
||||
// src の上から円盤を1枚取り出す
|
||||
pan := src.Back()
|
||||
// 円盤を tar の上に置く
|
||||
tar.PushBack(pan.Value)
|
||||
// `src` の最上部の円盤を取り外す
|
||||
src.Remove(pan)
|
||||
}
|
||||
|
||||
/* ハノイの塔の問題 f(i) を解く */
|
||||
func dfsHanota(i int, src, buf, tar *list.List) {
|
||||
// src に円盤が 1 枚だけ残っている場合は、そのまま tar へ移す
|
||||
if i == 1 {
|
||||
move(src, tar)
|
||||
return
|
||||
}
|
||||
// 部分問題 f(i-1):src の上部 i-1 枚の円盤を tar を補助にして buf へ移す
|
||||
dfsHanota(i-1, src, tar, buf)
|
||||
// 部分問題 f(1):src に残る 1 枚の円盤を tar に移す
|
||||
move(src, tar)
|
||||
// 部分問題 f(i-1):buf の上部 i-1 枚の円盤を src を補助にして tar へ移す
|
||||
dfsHanota(i-1, buf, src, tar)
|
||||
}
|
||||
|
||||
/* ハノイの塔を解く */
|
||||
func solveHanota(A, B, C *list.List) {
|
||||
n := A.Len()
|
||||
// A の上から n 枚の円盤を B を介して C へ移す
|
||||
dfsHanota(n, A, B, C)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// File: hanota_test.go
|
||||
// Created Time: 2023-07-21
|
||||
// Author: hongyun-robot (1836017030@qq.com)
|
||||
|
||||
package chapter_divide_and_conquer
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestHanota(t *testing.T) {
|
||||
// リスト末尾が柱の頂上
|
||||
A := list.New()
|
||||
for i := 5; i > 0; i-- {
|
||||
A.PushBack(i)
|
||||
}
|
||||
B := list.New()
|
||||
C := list.New()
|
||||
fmt.Println("初期状態:")
|
||||
fmt.Print("A = ")
|
||||
PrintList(A)
|
||||
fmt.Print("B = ")
|
||||
PrintList(B)
|
||||
fmt.Print("C = ")
|
||||
PrintList(C)
|
||||
|
||||
solveHanota(A, B, C)
|
||||
|
||||
fmt.Println("円盤の移動完了後:")
|
||||
fmt.Print("A = ")
|
||||
PrintList(A)
|
||||
fmt.Print("B = ")
|
||||
PrintList(B)
|
||||
fmt.Print("C = ")
|
||||
PrintList(C)
|
||||
}
|
||||
Reference in New Issue
Block a user