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,121 @@
// File: array_deque.go
// Created Time: 2023-03-13
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
import "fmt"
/* 循環配列ベースの両端キュー */
type arrayDeque struct {
nums []int // 両端キューの要素を格納する配列
front int // 先頭ポインタ。先頭要素を指す
queSize int // 両端キューの長さ
queCapacity int // キュー容量(格納できる要素数の上限)
}
/* キューを初期化 */
func newArrayDeque(queCapacity int) *arrayDeque {
return &arrayDeque{
nums: make([]int, queCapacity),
queCapacity: queCapacity,
front: 0,
queSize: 0,
}
}
/* 両端キューの長さを取得 */
func (q *arrayDeque) size() int {
return q.queSize
}
/* 両端キューが空かどうかを判定 */
func (q *arrayDeque) isEmpty() bool {
return q.queSize == 0
}
/* 循環配列のインデックスを計算 */
func (q *arrayDeque) index(i int) int {
// 剰余演算により配列の先頭と末尾をつなげる
// i が配列の末尾を越えたら先頭に戻る
// i が配列の先頭を越えて前に出たら末尾に戻る
return (i + q.queCapacity) % q.queCapacity
}
/* キュー先頭にエンキュー */
func (q *arrayDeque) pushFirst(num int) {
if q.queSize == q.queCapacity {
fmt.Println("両端キューは満杯です")
return
}
// 先頭ポインタを左に 1 つ移動する
// 剰余演算により、front が配列先頭を越えた後に末尾へ戻るようにする
q.front = q.index(q.front - 1)
// num をキュー先頭に追加
q.nums[q.front] = num
q.queSize++
}
/* キュー末尾にエンキュー */
func (q *arrayDeque) pushLast(num int) {
if q.queSize == q.queCapacity {
fmt.Println("両端キューは満杯です")
return
}
// キュー末尾ポインタを計算し、末尾インデックス + 1 を指す
rear := q.index(q.front + q.queSize)
// num をキュー末尾に追加
q.nums[rear] = num
q.queSize++
}
/* キュー先頭からデキュー */
func (q *arrayDeque) popFirst() any {
num := q.peekFirst()
if num == nil {
return nil
}
// 先頭ポインタを 1 つ後ろへ進める
q.front = q.index(q.front + 1)
q.queSize--
return num
}
/* キュー末尾からデキュー */
func (q *arrayDeque) popLast() any {
num := q.peekLast()
if num == nil {
return nil
}
q.queSize--
return num
}
/* キュー先頭の要素にアクセス */
func (q *arrayDeque) peekFirst() any {
if q.isEmpty() {
return nil
}
return q.nums[q.front]
}
/* キュー末尾の要素にアクセス */
func (q *arrayDeque) peekLast() any {
if q.isEmpty() {
return nil
}
// 末尾要素のインデックスを計算
last := q.index(q.front + q.queSize - 1)
return q.nums[last]
}
/* 表示用に Slice を取得 */
func (q *arrayDeque) toSlice() []int {
// 有効長の範囲内のリスト要素のみを変換
res := make([]int, q.queSize)
for i, j := 0, q.front; i < q.queSize; i++ {
res[i] = q.nums[q.index(j)]
j++
}
return res
}
@@ -0,0 +1,78 @@
// File: array_queue.go
// Created Time: 2022-11-28
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
/* 循環配列ベースのキュー */
type arrayQueue struct {
nums []int // キュー要素を格納する配列
front int // 先頭ポインタ。先頭要素を指す
queSize int // キューの長さ
queCapacity int // キュー容量(格納できる要素数の上限)
}
/* キューを初期化 */
func newArrayQueue(queCapacity int) *arrayQueue {
return &arrayQueue{
nums: make([]int, queCapacity),
queCapacity: queCapacity,
front: 0,
queSize: 0,
}
}
/* キューの長さを取得 */
func (q *arrayQueue) size() int {
return q.queSize
}
/* キューが空かどうかを判定 */
func (q *arrayQueue) isEmpty() bool {
return q.queSize == 0
}
/* エンキュー */
func (q *arrayQueue) push(num int) {
// rear == queCapacity のときキューは満杯
if q.queSize == q.queCapacity {
return
}
// 末尾ポインタを計算し、末尾インデックス + 1 を指す
// 剰余演算により、rear が配列末尾を越えた後に先頭へ戻るようにする
rear := (q.front + q.queSize) % q.queCapacity
// num をキュー末尾に追加
q.nums[rear] = num
q.queSize++
}
/* デキュー */
func (q *arrayQueue) pop() any {
num := q.peek()
if num == nil {
return nil
}
// 先頭ポインタを1つ後ろへ進め、末尾を越えたら配列先頭に戻す
q.front = (q.front + 1) % q.queCapacity
q.queSize--
return num
}
/* キュー先頭の要素にアクセス */
func (q *arrayQueue) peek() any {
if q.isEmpty() {
return nil
}
return q.nums[q.front]
}
/* 表示用に Slice を取得 */
func (q *arrayQueue) toSlice() []int {
rear := (q.front + q.queSize)
if rear >= q.queCapacity {
rear %= q.queCapacity
return append(q.nums[q.front:], q.nums[:rear]...)
}
return q.nums[q.front:rear]
}
@@ -0,0 +1,55 @@
// File: array_stack.go
// Created Time: 2022-11-26
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
/* 配列ベースのスタック */
type arrayStack struct {
data []int // データ
}
/* スタックを初期化 */
func newArrayStack() *arrayStack {
return &arrayStack{
// スタックの長さを 0、容量を 16 に設定
data: make([]int, 0, 16),
}
}
/* スタックの長さ */
func (s *arrayStack) size() int {
return len(s.data)
}
/* スタックが空かどうか */
func (s *arrayStack) isEmpty() bool {
return s.size() == 0
}
/* プッシュ */
func (s *arrayStack) push(v int) {
// スライスは自動で拡張される
s.data = append(s.data, v)
}
/* ポップ */
func (s *arrayStack) pop() any {
val := s.peek()
s.data = s.data[:len(s.data)-1]
return val
}
/* スタックトップ要素を取得する */
func (s *arrayStack) peek() any {
if s.isEmpty() {
return nil
}
val := s.data[len(s.data)-1]
return val
}
/* 表示用に Slice を取得 */
func (s *arrayStack) toSlice() []int {
return s.data
}
@@ -0,0 +1,141 @@
// File: deque_test.go
// Created Time: 2022-11-29
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
import (
"container/list"
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestDeque(t *testing.T) {
/* 両端キューを初期化 */
// Go では、list を両端キューとして使う
deque := list.New()
/* 要素をエンキュー */
deque.PushBack(2)
deque.PushBack(5)
deque.PushBack(4)
deque.PushFront(3)
deque.PushFront(1)
fmt.Print("両端キュー deque = ")
PrintList(deque)
/* 要素にアクセス */
front := deque.Front()
fmt.Println("先頭要素 front =", front.Value)
rear := deque.Back()
fmt.Println("末尾要素 rear =", rear.Value)
/* 要素をデキュー */
deque.Remove(front)
fmt.Print("先頭から取り出した要素 front = ", front.Value, "、取り出し後の deque = ")
PrintList(deque)
deque.Remove(rear)
fmt.Print("末尾から取り出した要素 rear = ", rear.Value, "、取り出し後の deque = ")
PrintList(deque)
/* 両端キューの長さを取得 */
size := deque.Len()
fmt.Println("両端キューの長さ size =", size)
/* 両端キューが空かどうかを判定 */
isEmpty := deque.Len() == 0
fmt.Println("両端キューが空か =", isEmpty)
}
func TestArrayDeque(t *testing.T) {
/* 両端キューを初期化 */
// Go では、list を両端キューとして使う
deque := newArrayDeque(16)
/* 要素をエンキュー */
deque.pushLast(3)
deque.pushLast(2)
deque.pushLast(5)
fmt.Print("両端キュー deque = ")
PrintSlice(deque.toSlice())
/* 要素にアクセス */
peekFirst := deque.peekFirst()
fmt.Println("先頭要素 peekFirst =", peekFirst)
peekLast := deque.peekLast()
fmt.Println("末尾要素 peekLast =", peekLast)
/* 要素をエンキュー */
deque.pushLast(4)
fmt.Print("要素 4 を末尾に追加した後 deque = ")
PrintSlice(deque.toSlice())
deque.pushFirst(1)
fmt.Print("要素 1 を先頭に追加した後 deque = ")
PrintSlice(deque.toSlice())
/* 要素をデキュー */
popFirst := deque.popFirst()
fmt.Print("先頭から取り出した要素 popFirst = ", popFirst, "、取り出し後の deque = ")
PrintSlice(deque.toSlice())
popLast := deque.popLast()
fmt.Print("末尾から取り出した要素 popLast = ", popLast, "、取り出し後の deque = ")
PrintSlice(deque.toSlice())
/* 両端キューの長さを取得 */
size := deque.size()
fmt.Println("両端キューの長さ size =", size)
/* 両端キューが空かどうかを判定 */
isEmpty := deque.isEmpty()
fmt.Println("両端キューが空か =", isEmpty)
}
func TestLinkedListDeque(t *testing.T) {
// キューを初期化
deque := newLinkedListDeque()
// 要素をエンキュー
deque.pushLast(2)
deque.pushLast(5)
deque.pushLast(4)
deque.pushFirst(3)
deque.pushFirst(1)
fmt.Print("キュー deque = ")
PrintList(deque.toList())
// キュー先頭の要素にアクセス
front := deque.peekFirst()
fmt.Println("先頭要素 front =", front)
rear := deque.peekLast()
fmt.Println("末尾要素 rear =", rear)
// 要素をデキュー
popFirst := deque.popFirst()
fmt.Print("先頭から取り出した要素 popFirst = ", popFirst, "、取り出し後の deque = ")
PrintList(deque.toList())
popLast := deque.popLast()
fmt.Print("末尾から取り出した要素 popLast = ", popLast, "、取り出し後の deque = ")
PrintList(deque.toList())
// キューの長さを取得
size := deque.size()
fmt.Println("キューの長さ size =", size)
// 空かどうかを判定
isEmpty := deque.isEmpty()
fmt.Println("キューが空か =", isEmpty)
}
// BenchmarkLinkedListDeque 67.92 ns/op in Mac M1 Pro
func BenchmarkLinkedListDeque(b *testing.B) {
deque := newLinkedListDeque()
// use b.N for looping
for i := 0; i < b.N; i++ {
deque.pushLast(777)
}
for i := 0; i < b.N; i++ {
deque.popFirst()
}
}
@@ -0,0 +1,85 @@
// File: linkedlist_deque.go
// Created Time: 2022-11-29
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
import (
"container/list"
)
/* 双方向連結リストベースの両端キュー */
type linkedListDeque struct {
// 組み込みパッケージ list を使う
data *list.List
}
/* 両端キューを初期化する */
func newLinkedListDeque() *linkedListDeque {
return &linkedListDeque{
data: list.New(),
}
}
/* キュー先頭に要素を追加する */
func (s *linkedListDeque) pushFirst(value any) {
s.data.PushFront(value)
}
/* キュー末尾に要素を追加する */
func (s *linkedListDeque) pushLast(value any) {
s.data.PushBack(value)
}
/* 先頭要素を取り出す */
func (s *linkedListDeque) popFirst() any {
if s.isEmpty() {
return nil
}
e := s.data.Front()
s.data.Remove(e)
return e.Value
}
/* 末尾要素を取り出す */
func (s *linkedListDeque) popLast() any {
if s.isEmpty() {
return nil
}
e := s.data.Back()
s.data.Remove(e)
return e.Value
}
/* キュー先頭の要素にアクセス */
func (s *linkedListDeque) peekFirst() any {
if s.isEmpty() {
return nil
}
e := s.data.Front()
return e.Value
}
/* キュー末尾の要素にアクセス */
func (s *linkedListDeque) peekLast() any {
if s.isEmpty() {
return nil
}
e := s.data.Back()
return e.Value
}
/* キューの長さを取得 */
func (s *linkedListDeque) size() int {
return s.data.Len()
}
/* キューが空かどうかを判定 */
func (s *linkedListDeque) isEmpty() bool {
return s.data.Len() == 0
}
/* 表示用に List を取得 */
func (s *linkedListDeque) toList() *list.List {
return s.data
}
@@ -0,0 +1,61 @@
// File: linkedlist_queue.go
// Created Time: 2022-11-28
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
import (
"container/list"
)
/* 連結リストベースのキュー */
type linkedListQueue struct {
// 組み込みパッケージ list でキューを実装する
data *list.List
}
/* キューを初期化 */
func newLinkedListQueue() *linkedListQueue {
return &linkedListQueue{
data: list.New(),
}
}
/* エンキュー */
func (s *linkedListQueue) push(value any) {
s.data.PushBack(value)
}
/* デキュー */
func (s *linkedListQueue) pop() any {
if s.isEmpty() {
return nil
}
e := s.data.Front()
s.data.Remove(e)
return e.Value
}
/* キュー先頭の要素にアクセス */
func (s *linkedListQueue) peek() any {
if s.isEmpty() {
return nil
}
e := s.data.Front()
return e.Value
}
/* キューの長さを取得 */
func (s *linkedListQueue) size() int {
return s.data.Len()
}
/* キューが空かどうかを判定 */
func (s *linkedListQueue) isEmpty() bool {
return s.data.Len() == 0
}
/* 表示用に List を取得 */
func (s *linkedListQueue) toList() *list.List {
return s.data
}
@@ -0,0 +1,61 @@
// File: linkedlist_stack.go
// Created Time: 2022-11-28
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
import (
"container/list"
)
/* 連結リストベースのスタック */
type linkedListStack struct {
// 組み込みパッケージ list でスタックを実装する
data *list.List
}
/* スタックを初期化 */
func newLinkedListStack() *linkedListStack {
return &linkedListStack{
data: list.New(),
}
}
/* プッシュ */
func (s *linkedListStack) push(value int) {
s.data.PushBack(value)
}
/* ポップ */
func (s *linkedListStack) pop() any {
if s.isEmpty() {
return nil
}
e := s.data.Back()
s.data.Remove(e)
return e.Value
}
/* スタックトップの要素にアクセス */
func (s *linkedListStack) peek() any {
if s.isEmpty() {
return nil
}
e := s.data.Back()
return e.Value
}
/* スタックの長さを取得 */
func (s *linkedListStack) size() int {
return s.data.Len()
}
/* スタックが空かどうかを判定 */
func (s *linkedListStack) isEmpty() bool {
return s.data.Len() == 0
}
/* 表示用に List を取得 */
func (s *linkedListStack) toList() *list.List {
return s.data
}
@@ -0,0 +1,146 @@
// File: queue_test.go
// Created Time: 2022-11-28
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
import (
"container/list"
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestQueue(t *testing.T) {
/* キューを初期化 */
// Go では、list をキューとして使う
queue := list.New()
/* 要素をエンキュー */
queue.PushBack(1)
queue.PushBack(3)
queue.PushBack(2)
queue.PushBack(5)
queue.PushBack(4)
fmt.Print("キュー queue = ")
PrintList(queue)
/* キュー先頭の要素にアクセス */
peek := queue.Front()
fmt.Println("先頭要素 peek =", peek.Value)
/* 要素をデキュー */
pop := queue.Front()
queue.Remove(pop)
fmt.Print("取り出した要素 pop = ", pop.Value, "、取り出し後の queue = ")
PrintList(queue)
/* キューの長さを取得 */
size := queue.Len()
fmt.Println("キューの長さ size =", size)
/* キューが空かどうかを判定 */
isEmpty := queue.Len() == 0
fmt.Println("キューが空か =", isEmpty)
}
func TestArrayQueue(t *testing.T) {
// キューを初期化し、キューの共通インターフェースを使う
capacity := 10
queue := newArrayQueue(capacity)
if queue.pop() != nil {
t.Errorf("want:%v,got:%v", nil, queue.pop())
}
// 要素をエンキュー
queue.push(1)
queue.push(3)
queue.push(2)
queue.push(5)
queue.push(4)
fmt.Print("キュー queue = ")
PrintSlice(queue.toSlice())
// キュー先頭の要素にアクセス
peek := queue.peek()
fmt.Println("先頭要素 peek =", peek)
// 要素をデキュー
pop := queue.pop()
fmt.Print("取り出した要素 pop = ", pop, ", 取り出し後 queue = ")
PrintSlice(queue.toSlice())
// キューの長さを取得
size := queue.size()
fmt.Println("キューの長さ size =", size)
// 空かどうかを判定
isEmpty := queue.isEmpty()
fmt.Println("キューが空か =", isEmpty)
/* 循環配列をテストする */
for i := 0; i < 10; i++ {
queue.push(i)
queue.pop()
fmt.Print("第", i, "回のエンキュー + デキュー後 queue =")
PrintSlice(queue.toSlice())
}
}
func TestLinkedListQueue(t *testing.T) {
// キューを初期化する
queue := newLinkedListQueue()
// 要素をエンキュー
queue.push(1)
queue.push(3)
queue.push(2)
queue.push(5)
queue.push(4)
fmt.Print("キュー queue = ")
PrintList(queue.toList())
// キュー先頭の要素にアクセス
peek := queue.peek()
fmt.Println("先頭要素 peek =", peek)
// 要素をデキュー
pop := queue.pop()
fmt.Print("取り出した要素 pop = ", pop, ", 取り出し後 queue = ")
PrintList(queue.toList())
// キューの長さを取得
size := queue.size()
fmt.Println("キューの長さ size =", size)
// 空かどうかを判定
isEmpty := queue.isEmpty()
fmt.Println("キューが空か =", isEmpty)
}
// BenchmarkArrayQueue 8 ns/op in Mac M1 Pro
func BenchmarkArrayQueue(b *testing.B) {
capacity := 1000
queue := newArrayQueue(capacity)
// use b.N for looping
for i := 0; i < b.N; i++ {
queue.push(777)
}
for i := 0; i < b.N; i++ {
queue.pop()
}
}
// BenchmarkLinkedQueue 62.66 ns/op in Mac M1 Pro
func BenchmarkLinkedQueue(b *testing.B) {
queue := newLinkedListQueue()
// use b.N for looping
for i := 0; i < b.N; i++ {
queue.push(777)
}
for i := 0; i < b.N; i++ {
queue.pop()
}
}
@@ -0,0 +1,130 @@
// File: stack_test.go
// Created Time: 2022-11-28
// Author: Reanon (793584285@qq.com)
package chapter_stack_and_queue
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestStack(t *testing.T) {
/* スタックを初期化 */
// Go では、Slice をスタックとして使うことが推奨される
var stack []int
/* 要素をプッシュ */
stack = append(stack, 1)
stack = append(stack, 3)
stack = append(stack, 2)
stack = append(stack, 5)
stack = append(stack, 4)
fmt.Print("スタック stack = ")
PrintSlice(stack)
/* スタックトップの要素にアクセス */
peek := stack[len(stack)-1]
fmt.Println("スタックトップ要素 peek =", peek)
/* 要素をポップ */
pop := stack[len(stack)-1]
stack = stack[:len(stack)-1]
fmt.Print("ポップした要素 pop = ", pop, ",ポップ後 stack = ")
PrintSlice(stack)
/* スタックの長さを取得 */
size := len(stack)
fmt.Println("スタックの長さ size =", size)
/* 空かどうかを判定 */
isEmpty := len(stack) == 0
fmt.Println("スタックが空かどうか =", isEmpty)
}
func TestArrayStack(t *testing.T) {
// スタックを初期化し、インターフェース型で受ける
stack := newArrayStack()
// 要素をプッシュ
stack.push(1)
stack.push(3)
stack.push(2)
stack.push(5)
stack.push(4)
fmt.Print("スタック stack = ")
PrintSlice(stack.toSlice())
// スタックトップの要素にアクセス
peek := stack.peek()
fmt.Println("スタックトップ要素 peek =", peek)
// 要素をポップ
pop := stack.pop()
fmt.Print("ポップした要素 pop = ", pop, ", ポップ後 stack = ")
PrintSlice(stack.toSlice())
// スタックの長さを取得
size := stack.size()
fmt.Println("スタックの長さ size =", size)
// 空かどうかを判定
isEmpty := stack.isEmpty()
fmt.Println("スタックが空かどうか =", isEmpty)
}
func TestLinkedListStack(t *testing.T) {
// スタックを初期化
stack := newLinkedListStack()
// 要素をプッシュ
stack.push(1)
stack.push(3)
stack.push(2)
stack.push(5)
stack.push(4)
fmt.Print("スタック stack = ")
PrintList(stack.toList())
// スタックトップの要素にアクセス
peek := stack.peek()
fmt.Println("スタックトップ要素 peek =", peek)
// 要素をポップ
pop := stack.pop()
fmt.Print("ポップした要素 pop = ", pop, ", ポップ後 stack = ")
PrintList(stack.toList())
// スタックの長さを取得
size := stack.size()
fmt.Println("スタックの長さ size =", size)
// 空かどうかを判定
isEmpty := stack.isEmpty()
fmt.Println("スタックが空かどうか =", isEmpty)
}
// BenchmarkArrayStack 8 ns/op in Mac M1 Pro
func BenchmarkArrayStack(b *testing.B) {
stack := newArrayStack()
// use b.N for looping
for i := 0; i < b.N; i++ {
stack.push(777)
}
for i := 0; i < b.N; i++ {
stack.pop()
}
}
// BenchmarkLinkedListStack 65.02 ns/op in Mac M1 Pro
func BenchmarkLinkedListStack(b *testing.B) {
stack := newLinkedListStack()
// use b.N for looping
for i := 0; i < b.N; i++ {
stack.push(777)
}
for i := 0; i < b.N; i++ {
stack.pop()
}
}