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,65 @@
/*
* File: binary_search.rs
* Created Time: 2023-02-05
* Author: codingonion (coderonion@gmail.com)
*/
/* 二分探索(両閉区間) */
fn binary_search(nums: &[i32], target: i32) -> i32 {
// 両閉区間 [0, n-1] を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素を指す
let mut i = 0;
let mut j = nums.len() as i32 - 1;
// ループし、探索区間が空になったら終了する(i > j で空)
while i <= j {
let m = i + (j - i) / 2; // 中点インデックス m を計算
if nums[m as usize] < target {
// この場合、target は区間 [m+1, j] にある
i = m + 1;
} else if nums[m as usize] > target {
// この場合、target は区間 [i, m-1] にある
j = m - 1;
} else {
// 目標要素が見つかったらそのインデックスを返す
return m;
}
}
// 目標要素が見つからなければ -1 を返す
return -1;
}
/* 二分探索(左閉右開区間) */
fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
// 左閉右開区間 [0, n) を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素+1を指す
let mut i = 0;
let mut j = nums.len() as i32;
// ループし、探索区間が空になったら終了する(i = j で空)
while i < j {
let m = i + (j - i) / 2; // 中点インデックス m を計算
if nums[m as usize] < target {
// この場合、target は区間 [m+1, j) にある
i = m + 1;
} else if nums[m as usize] > target {
// この場合、target は区間 [i, m) にある
j = m;
} else {
// 目標要素が見つかったらそのインデックスを返す
return m;
}
}
// 目標要素が見つからなければ -1 を返す
return -1;
}
/* Driver Code */
pub fn main() {
let target = 6;
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
// 二分探索(両閉区間)
let mut index = binary_search(&nums, target);
println!("目的の要素 6 のインデックス = {index}");
// 二分探索(左閉右開区間)
index = binary_search_lcro(&nums, target);
println!("目的の要素 6 のインデックス = {index}");
}
@@ -0,0 +1,50 @@
/*
* File: binary_search_edge.rs
* Created Time: 2023-08-30
* Author: night-cruise (2586447362@qq.com)
*/
mod binary_search_insertion;
use binary_search_insertion::binary_search_insertion;
/* 最も左の target を二分探索 */
fn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {
// target の挿入位置を探すのと等価
let i = binary_search_insertion(nums, target);
// target が見つからなければ、-1 を返す
if i == nums.len() as i32 || nums[i as usize] != target {
return -1;
}
// target が見つかったら、インデックス i を返す
i
}
/* 最も右の target を二分探索 */
fn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {
// 最左の target + 1 を探す問題に変換する
let i = binary_search_insertion(nums, target + 1);
// j は最も右の target を指し、i は target より大きい最初の要素を指す
let j = i - 1;
// target が見つからなければ、-1 を返す
if j == -1 || nums[j as usize] != target {
return -1;
}
// target が見つかったら、インデックス j を返す
j
}
/* Driver Code */
fn main() {
// 重複要素を含む配列
let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
println!("\n配列 nums = {:?}", nums);
// 二分探索で左端と右端を探す
for target in [6, 7] {
let index = binary_search_left_edge(&nums, target);
println!("最も左にある要素 {} のインデックスは {}", target, index);
let index = binary_search_right_edge(&nums, target);
println!("最も右にある要素 {} のインデックスは {}", target, index);
}
}
@@ -0,0 +1,61 @@
/*
* File: binary_search_insertion.rs
* Created Time: 2023-08-30
* Author: night-cruise (2586447362@qq.com)
*/
#![allow(unused)]
/* 二分探索で挿入位置を探す(重複要素なし) */
fn binary_search_insertion_simple(nums: &[i32], target: i32) -> i32 {
let (mut i, mut j) = (0, nums.len() as i32 - 1); // 両閉区間 [0, n-1] を初期化
while i <= j {
let m = i + (j - i) / 2; // 中点インデックス m を計算
if nums[m as usize] < target {
i = m + 1; // target は区間 [m+1, j] にある
} else if nums[m as usize] > target {
j = m - 1; // target は区間 [i, m-1] にある
} else {
return m;
}
}
// target が見つからなければ、挿入位置 i を返す
i
}
/* 二分探索で挿入位置を探す(重複要素あり) */
pub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {
let (mut i, mut j) = (0, nums.len() as i32 - 1); // 両閉区間 [0, n-1] を初期化
while i <= j {
let m = i + (j - i) / 2; // 中点インデックス m を計算
if nums[m as usize] < target {
i = m + 1; // target は区間 [m+1, j] にある
} else if nums[m as usize] > target {
j = m - 1; // target は区間 [i, m-1] にある
} else {
j = m - 1; // target より小さい最初の要素は区間 [i, m-1] にある
}
}
// 挿入位置 i を返す
i
}
/* Driver Code */
fn main() {
// 重複要素のない配列
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
println!("\n配列 nums = {:?}", nums);
// 二分探索で挿入位置を探す
for target in [6, 9] {
let index = binary_search_insertion_simple(&nums, target);
println!("要素 {} の挿入位置のインデックスは {}", target, index);
}
// 重複要素を含む配列
let nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
println!("\n配列 nums = {:?}", nums);
// 二分探索で挿入位置を探す
for target in [2, 6, 20] {
let index = binary_search_insertion(&nums, target);
println!("要素 {} の挿入位置のインデックスは {}", target, index);
}
}
@@ -0,0 +1,50 @@
/*
* File: hashing_search.rs
* Created Time: 2023-07-09
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::ListNode;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
/* ハッシュ探索(配列) */
fn hashing_search_array<'a>(map: &'a HashMap<i32, usize>, target: i32) -> Option<&'a usize> {
// ハッシュテーブルの key: 対象要素、value: インデックス
// ハッシュテーブルにその key がなければ None を返す
map.get(&target)
}
/* ハッシュ探索(連結リスト) */
fn hashing_search_linked_list(
map: &HashMap<i32, Rc<RefCell<ListNode<i32>>>>,
target: i32,
) -> Option<&Rc<RefCell<ListNode<i32>>>> {
// ハッシュテーブルの key: 対象ノードの値、value: ノードオブジェクト
// ハッシュテーブルにその key がなければ None を返す
map.get(&target)
}
/* Driver Code */
pub fn main() {
let target = 3;
/* ハッシュ探索(配列) */
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// ハッシュテーブルを初期化
let mut map = HashMap::new();
for (i, num) in nums.iter().enumerate() {
map.insert(*num, i); // key: 要素、value: インデックス
}
let index = hashing_search_array(&map, target);
println!("対象要素 3 のインデックス = {}", index.unwrap());
/* ハッシュ探索(連結リスト) */
let head = ListNode::arr_to_linked_list(&nums);
// ハッシュテーブルを初期化する
// let mut map1 = HashMap::new();
let map1 = ListNode::linked_list_to_hashmap(head);
let node = hashing_search_linked_list(&map1, target);
println!("対象ノード値 3 に対応するノードオブジェクトは {:?}", node);
}
@@ -0,0 +1,54 @@
/*
* File: linear_search.rs
* Created Time: 2023-07-09
* Author: codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::ListNode;
use std::cell::RefCell;
use std::rc::Rc;
/* 線形探索(配列) */
fn linear_search_array(nums: &[i32], target: i32) -> i32 {
// 配列を走査
for (i, num) in nums.iter().enumerate() {
// 目標要素が見つかったらそのインデックスを返す
if num == &target {
return i as i32;
}
}
// 目標要素が見つからなければ -1 を返す
return -1;
}
/* 線形探索(連結リスト) */
fn linear_search_linked_list(
head: Rc<RefCell<ListNode<i32>>>,
target: i32,
) -> Option<Rc<RefCell<ListNode<i32>>>> {
// 対象ノードが見つかったら、それを返す
if head.borrow().val == target {
return Some(head);
};
// 対象ノードが見つかったら、それを返す
if let Some(node) = &head.borrow_mut().next {
return linear_search_linked_list(node.clone(), target);
}
// 対象ノードが見つからない場合は None を返す
return None;
}
/* Driver Code */
pub fn main() {
let target = 3;
/* 配列で線形探索を行う */
let nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
let index = linear_search_array(&nums, target);
println!("対象要素 3 のインデックス = {}", index);
/* 連結リストで線形探索を行う */
let head = ListNode::arr_to_linked_list(&nums);
let node = linear_search_linked_list(head.unwrap(), target);
println!("対象ノード値 3 に対応するノードオブジェクトは {:?}", node);
}
@@ -0,0 +1,52 @@
/*
* File: two_sum.rs
* Created Time: 2023-01-14
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
*/
use hello_algo_rust::include::print_util;
use std::collections::HashMap;
/* 方法 1:総当たり列挙 */
pub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
let size = nums.len();
// 2重ループのため、時間計算量は O(n^2)
for i in 0..size - 1 {
for j in i + 1..size {
if nums[i] + nums[j] == target {
return Some(vec![i as i32, j as i32]);
}
}
}
None
}
/* 方法 2:補助ハッシュテーブル */
pub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {
// 補助ハッシュテーブルを使用し、空間計算量は O(n)
let mut dic = HashMap::new();
// 単一ループで、時間計算量は O(n)
for (i, num) in nums.iter().enumerate() {
match dic.get(&(target - num)) {
Some(v) => return Some(vec![*v as i32, i as i32]),
None => dic.insert(num, i as i32),
};
}
None
}
fn main() {
// ======= Test Case =======
let nums = vec![2, 7, 11, 15];
let target = 13;
// ====== Driver Code ======
// 方法 1
let res = two_sum_brute_force(&nums, target).unwrap();
print!("方法1 res = ");
print_util::print_array(&res);
// 方法 2
let res = two_sum_hash_table(&nums, target).unwrap();
print!("\n方法2 res = ");
print_util::print_array(&res);
}