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,91 @@
// File: iteration.zig
// Created Time: 2023-09-27
// Author: QiLOL (pikaqqpika@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
const std = @import("std");
const Allocator = std.mem.Allocator;
// for ループ
fn forLoop(n: usize) i32 {
var res: i32 = 0;
// 1, 2, ..., n-1, n を順に加算する
for (1..n + 1) |i| {
res += @intCast(i);
}
return res;
}
// while ループ
fn whileLoop(n: i32) i32 {
var res: i32 = 0;
var i: i32 = 1; // 条件変数を初期化する
// 1, 2, ..., n-1, n を順に加算する
while (i <= n) : (i += 1) {
res += @intCast(i);
}
return res;
}
// while ループ(2回更新)
fn whileLoopII(n: i32) i32 {
var res: i32 = 0;
var i: i32 = 1; // 条件変数を初期化する
// 1, 4, 10, ... を順に加算する
while (i <= n) : ({
// 条件変数を更新する
i += 1;
i *= 2;
}) {
res += @intCast(i);
}
return res;
}
// 二重 for ループ
fn nestedForLoop(allocator: Allocator, n: usize) ![]const u8 {
var res = std.ArrayList(u8).init(allocator);
defer res.deinit();
var buffer: [20]u8 = undefined;
// i = 1, 2, ..., n-1, n とループする
for (1..n + 1) |i| {
// j = 1, 2, ..., n-1, n とループする
for (1..n + 1) |j| {
const str = try std.fmt.bufPrint(&buffer, "({d}, {d}), ", .{ i, j });
try res.appendSlice(str);
}
}
return res.toOwnedSlice();
}
// Driver Code
pub fn run() !void {
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const n: i32 = 5;
var res: i32 = 0;
res = forLoop(n);
std.debug.print("for ループの合計結果 res = {}\n", .{res});
res = whileLoop(n);
std.debug.print("while ループの合計結果 res = {}\n", .{res});
res = whileLoopII(n);
std.debug.print("while ループ(2 回更新)の合計結果 res = {}\n", .{res});
const resStr = try nestedForLoop(allocator, n);
std.debug.print("二重 for ループの走査結果 {s}\n", .{resStr});
allocator.free(resStr);
std.debug.print("\n", .{});
}
pub fn main() !void {
try run();
}
test "interation" {
try run();
}
@@ -0,0 +1,88 @@
// File: recursion.zig
// Created Time: 2023-09-27
// Author: QiLOL (pikaqqpika@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
const std = @import("std");
// 再帰関数
fn recur(n: i32) i32 {
// 終了条件
if (n == 1) {
return 1;
}
// 再帰:再帰呼び出し
const res = recur(n - 1);
// 帰りがけ:結果を返す
return n + res;
}
// 反復で再帰を模擬する
fn forLoopRecur(comptime n: i32) i32 {
// 明示的なスタックを使ってシステムコールスタックを模擬する
var stack: [n]i32 = undefined;
var res: i32 = 0;
// 再帰:再帰呼び出し
var i: usize = n;
while (i > 0) {
stack[i - 1] = @intCast(i);
i -= 1;
}
// 帰りがけ:結果を返す
var index: usize = n;
while (index > 0) {
index -= 1;
res += stack[index];
}
// res = 1+2+3+...+n
return res;
}
// 末尾再帰関数
fn tailRecur(n: i32, res: i32) i32 {
// 終了条件
if (n == 0) {
return res;
}
// 末尾再帰呼び出し
return tailRecur(n - 1, res + n);
}
// フィボナッチ数列
fn fib(n: i32) i32 {
// 終了条件 f(1) = 0, f(2) = 1
if (n == 1 or n == 2) {
return n - 1;
}
// f(n) = f(n-1) + f(n-2) を再帰的に呼び出す
const res: i32 = fib(n - 1) + fib(n - 2);
// 結果 f(n) を返す
return res;
}
// Driver Code
pub fn run() void {
const n: i32 = 5;
var res: i32 = 0;
res = recur(n);
std.debug.print("再帰関数の合計結果 res = {}\n", .{recur(n)});
res = forLoopRecur(n);
std.debug.print("反復で再帰をシミュレートした合計結果 res = {}\n", .{forLoopRecur(n)});
res = tailRecur(n, 0);
std.debug.print("末尾再帰関数の合計結果 res = {}\n", .{tailRecur(n, 0)});
res = fib(n);
std.debug.print("フィボナッチ数列の第 {} 項は {}\n", .{ n, fib(n) });
std.debug.print("\n", .{});
}
pub fn main() void {
run();
}
test "recursion" {
run();
}
@@ -0,0 +1,142 @@
// File: space_complexity.zig
// Created Time: 2023-01-07
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
const std = @import("std");
const utils = @import("utils");
const ListNode = utils.ListNode;
const TreeNode = utils.TreeNode;
// 関数
fn function() i32 {
// 何らかの処理を行う
return 0;
}
// 定数階
fn constant(n: i32) void {
// 定数、変数、オブジェクトは O(1) の空間を占める
const a: i32 = 0;
const b: i32 = 0;
const nums = [_]i32{0} ** 10000;
const node = ListNode(i32){ .val = 0 };
var i: i32 = 0;
// ループ内の変数は O(1) の空間を占める
while (i < n) : (i += 1) {
const c: i32 = 0;
_ = c;
}
// ループ内の関数は O(1) の空間を占める
i = 0;
while (i < n) : (i += 1) {
_ = function();
}
_ = a;
_ = b;
_ = nums;
_ = node;
}
// 線形階
fn linear(comptime n: i32) !void {
// 長さ n の配列は O(n) の空間を使用
const nums = [_]i32{0} ** n;
// 長さ n のリストは O(n) の空間を使用
var nodes = std.ArrayList(i32).init(std.heap.page_allocator);
defer nodes.deinit();
var i: i32 = 0;
while (i < n) : (i += 1) {
try nodes.append(i);
}
// 長さ n のハッシュテーブルは O(n) の空間を使用
var map = std.AutoArrayHashMap(i32, []const u8).init(std.heap.page_allocator);
defer map.deinit();
var j: i32 = 0;
while (j < n) : (j += 1) {
const string = try std.fmt.allocPrint(std.heap.page_allocator, "{d}", .{j});
defer std.heap.page_allocator.free(string);
try map.put(i, string);
}
_ = nums;
}
// 線形時間(再帰実装)
fn linearRecur(comptime n: i32) void {
std.debug.print("再帰 n = {}\n", .{n});
if (n == 1) return;
linearRecur(n - 1);
}
// 二乗階
fn quadratic(n: i32) !void {
// 二次元リストは O(n^2) の空間を使用
var nodes = std.ArrayList(std.ArrayList(i32)).init(std.heap.page_allocator);
defer nodes.deinit();
var i: i32 = 0;
while (i < n) : (i += 1) {
var tmp = std.ArrayList(i32).init(std.heap.page_allocator);
defer tmp.deinit();
var j: i32 = 0;
while (j < n) : (j += 1) {
try tmp.append(0);
}
try nodes.append(tmp);
}
}
// 二次時間(再帰実装)
fn quadraticRecur(comptime n: i32) i32 {
if (n <= 0) return 0;
const nums = [_]i32{0} ** n;
std.debug.print("再帰 n = {} における nums の長さ = {}\n", .{ n, nums.len });
return quadraticRecur(n - 1);
}
// 指数時間(完全二分木の構築)
fn buildTree(allocator: std.mem.Allocator, n: i32) !?*TreeNode(i32) {
if (n == 0) return null;
const root = try allocator.create(TreeNode(i32));
root.init(0);
root.left = try buildTree(allocator, n - 1);
root.right = try buildTree(allocator, n - 1);
return root;
}
// 木のメモリを解放する
fn freeTree(allocator: std.mem.Allocator, root: ?*const TreeNode(i32)) void {
if (root == null) return;
freeTree(allocator, root.?.left);
freeTree(allocator, root.?.right);
allocator.destroy(root.?);
}
// Driver Code
pub fn run() !void {
var gpa = std.heap.DebugAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const n: i32 = 5;
// 定数階
constant(n);
// 線形階
try linear(n);
linearRecur(n);
// 二乗階
try quadratic(n);
_ = quadraticRecur(n);
// 指数オーダー
const root = try buildTree(allocator, n);
defer freeTree(allocator, root);
std.debug.print("{}\n", .{utils.fmt.tree(i32, root)});
std.debug.print("\n", .{});
}
pub fn main() !void {
try run();
}
test "space_complexity" {
try run();
}
@@ -0,0 +1,184 @@
// File: time_complexity.zig
// Created Time: 2022-12-28
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
const std = @import("std");
// 定数階
fn constant(n: i32) i32 {
_ = n;
var count: i32 = 0;
const size: i32 = 100_000;
var i: i32 = 0;
while (i < size) : (i += 1) {
count += 1;
}
return count;
}
// 線形階
fn linear(n: i32) i32 {
var count: i32 = 0;
var i: i32 = 0;
while (i < n) : (i += 1) {
count += 1;
}
return count;
}
// 線形時間(配列を走査)
fn arrayTraversal(nums: []i32) i32 {
var count: i32 = 0;
// ループ回数は配列長に比例する
for (nums) |_| {
count += 1;
}
return count;
}
// 二乗階
fn quadratic(n: i32) i32 {
var count: i32 = 0;
var i: i32 = 0;
// ループ回数はデータサイズ n の二乗に比例する
while (i < n) : (i += 1) {
var j: i32 = 0;
while (j < n) : (j += 1) {
count += 1;
}
}
return count;
}
// 二次時間(バブルソート)
fn bubbleSort(nums: []i32) i32 {
var count: i32 = 0; // カウンタ
// 外側のループ:未ソート区間は [0, i]
var i: i32 = @as(i32, @intCast(nums.len)) - 1;
while (i > 0) : (i -= 1) {
var j: usize = 0;
// 内側のループ:未ソート区間 [0, i] の最大要素をその区間の最右端へ交換
while (j < i) : (j += 1) {
if (nums[j] > nums[j + 1]) {
// nums[j] と nums[j + 1] を交換
const tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
count += 3; // 要素交換には 3 回の単位操作が含まれる
}
}
}
return count;
}
// 指数時間(ループ実装)
fn exponential(n: i32) i32 {
var count: i32 = 0;
var bas: i32 = 1;
var i: i32 = 0;
// 細胞は各ラウンドで 2 つに分裂し、数列 1, 2, 4, 8, ..., 2^(n-1) を形成する
while (i < n) : (i += 1) {
var j: i32 = 0;
while (j < bas) : (j += 1) {
count += 1;
}
bas *= 2;
}
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
return count;
}
// 指数時間(再帰実装)
fn expRecur(n: i32) i32 {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
// 対数時間(ループ実装)
fn logarithmic(n: i32) i32 {
var count: i32 = 0;
var n_var: i32 = n;
while (n_var > 1) : (n_var = @divTrunc(n_var, 2)) {
count += 1;
}
return count;
}
// 対数時間(再帰実装)
fn logRecur(n: i32) i32 {
if (n <= 1) return 0;
return logRecur(@divTrunc(n, 2)) + 1;
}
// 線形対数時間
fn linearLogRecur(n: i32) i32 {
if (n <= 1) return 1;
var count: i32 = linearLogRecur(@divTrunc(n, 2)) + linearLogRecur(@divTrunc(n, 2));
var i: i32 = 0;
while (i < n) : (i += 1) {
count += 1;
}
return count;
}
// 階乗時間(再帰実装)
fn factorialRecur(n: i32) i32 {
if (n == 0) return 1;
var count: i32 = 0;
var i: i32 = 0;
// 1個から n 個に分裂
while (i < n) : (i += 1) {
count += factorialRecur(n - 1);
}
return count;
}
// Driver Code
pub fn run() void {
// n を変えて実行し、各計算量で操作回数がどう変化するかを確認できる
const n: i32 = 8;
std.debug.print("入力データサイズ n = {}\n", .{n});
var count = constant(n);
std.debug.print("定数オーダーの操作数 = {}\n", .{count});
count = linear(n);
std.debug.print("線形オーダーの操作数 = {}\n", .{count});
var nums = [_]i32{0} ** n;
count = arrayTraversal(&nums);
std.debug.print("線形オーダー(配列走査)の操作数 = {}\n", .{count});
count = quadratic(n);
std.debug.print("二乗オーダーの操作数 = {}\n", .{count});
for (&nums, 0..) |*num, i| {
num.* = n - @as(i32, @intCast(i)); // [n,n-1,...,2,1]
}
count = bubbleSort(&nums);
std.debug.print("二乗オーダー(バブルソート)の操作数 = {}\n", .{count});
count = exponential(n);
std.debug.print("指数オーダー(ループ実装)の操作数 = {}\n", .{count});
count = expRecur(n);
std.debug.print("指数オーダー(再帰実装)の操作数 = {}\n", .{count});
count = logarithmic(n);
std.debug.print("対数オーダー(ループ実装)の操作数 = {}\n", .{count});
count = logRecur(n);
std.debug.print("対数オーダー(再帰実装)の操作数 = {}\n", .{count});
count = linearLogRecur(n);
std.debug.print("線形対数オーダー(再帰実装)の操作数 = {}\n", .{count});
count = factorialRecur(n);
std.debug.print("階乗オーダー(再帰実装)の操作数 = {}\n", .{count});
std.debug.print("\n", .{});
}
pub fn main() !void {
run();
}
test "time_complexity" {
run();
}
@@ -0,0 +1,53 @@
// File: worst_best_time_complexity.zig
// Created Time: 2022-12-28
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
const std = @import("std");
const utils = @import("utils");
// 要素が { 1, 2, ..., n } で、順序がシャッフルされた配列を生成
pub fn randomNumbers(comptime n: usize) [n]i32 {
var nums: [n]i32 = undefined;
// 配列 nums = { 1, 2, 3, ..., n } を生成
for (&nums, 0..) |*num, i| {
num.* = @as(i32, @intCast(i)) + 1;
}
// 配列要素をランダムにシャッフル
const rand = std.crypto.random;
rand.shuffle(i32, &nums);
return nums;
}
// 配列 nums 内で数値 1 のインデックスを探す
pub fn findOne(nums: []i32) i32 {
for (nums, 0..) |num, i| {
// 要素 1 が配列の先頭にあるとき、最良時間計算量 O(1) となる
// 要素 1 が配列の末尾にあるとき、最悪時間計算量 O(n) となる
if (num == 1) return @intCast(i);
}
return -1;
}
// Driver Code
pub fn run() void {
var i: i32 = 0;
while (i < 10) : (i += 1) {
const n: usize = 100;
var nums = randomNumbers(n);
const index = findOne(&nums);
std.debug.print("配列 [ 1, 2, ..., n ] をシャッフルした後 = ", .{});
std.debug.print("{}\n", .{utils.fmt.slice(nums)});
std.debug.print("数字 1 のインデックスは {}\n", .{index});
}
std.debug.print("\n", .{});
}
pub fn main() !void {
run();
}
test "worst_best_time_complexity" {
run();
}