mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 22:16:06 +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,111 @@
|
||||
/*
|
||||
* File: array.rs
|
||||
* Created Time: 2023-01-15
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
use rand::Rng;
|
||||
|
||||
/* 要素へランダムアクセス */
|
||||
fn random_access(nums: &[i32]) -> i32 {
|
||||
// 区間 [0, nums.len()) からランダムに数字を 1 つ選ぶ
|
||||
let random_index = rand::thread_rng().gen_range(0..nums.len());
|
||||
// ランダムな要素を取得して返す
|
||||
let random_num = nums[random_index];
|
||||
random_num
|
||||
}
|
||||
|
||||
/* 配列長を拡張する */
|
||||
fn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {
|
||||
// 拡張後の長さを持つ配列を初期化する
|
||||
let mut res: Vec<i32> = vec![0; nums.len() + enlarge];
|
||||
// 元の配列の全要素を新しい配列にコピー
|
||||
res[0..nums.len()].copy_from_slice(nums);
|
||||
|
||||
// 拡張後の新しい配列を返す
|
||||
res
|
||||
}
|
||||
|
||||
/* 配列の index 番目に要素 num を挿入 */
|
||||
fn insert(nums: &mut [i32], num: i32, index: usize) {
|
||||
// インデックス index 以降の全要素を 1 つ後ろへ移動する
|
||||
for i in (index + 1..nums.len()).rev() {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// index の要素に num を代入する
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* index の要素を削除する */
|
||||
fn remove(nums: &mut [i32], index: usize) {
|
||||
// インデックス index より後ろの全要素を 1 つ前へ移動する
|
||||
for i in index..nums.len() - 1 {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 配列を走査 */
|
||||
fn traverse(nums: &[i32]) {
|
||||
let mut _count = 0;
|
||||
// インデックスで配列を走査
|
||||
for i in 0..nums.len() {
|
||||
_count += nums[i];
|
||||
}
|
||||
// 配列要素を直接走査
|
||||
_count = 0;
|
||||
for &num in nums {
|
||||
_count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* 配列内で指定要素を探す */
|
||||
fn find(nums: &[i32], target: i32) -> Option<usize> {
|
||||
for i in 0..nums.len() {
|
||||
if nums[i] == target {
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 配列を初期化 */
|
||||
let arr: [i32; 5] = [0; 5];
|
||||
print!("配列 arr = ");
|
||||
print_util::print_array(&arr);
|
||||
// Rust では、長さを指定する場合([i32; 5])は配列、指定しない場合(&[i32])はスライスである
|
||||
// Rust の配列はコンパイル時に長さが確定するよう設計されているため、長さには定数しか使えない
|
||||
// Vector は Rust で通常動的配列として使われる型である
|
||||
// extend() メソッドを実装しやすくするため、以下では vector を配列(array)として扱う
|
||||
let nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
print!("\n配列 nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// ランダムアクセス
|
||||
let random_num = random_access(&nums);
|
||||
println!("\nnums からランダムな要素 {} を取得", random_num);
|
||||
|
||||
// 長さを拡張
|
||||
let mut nums: Vec<i32> = extend(&nums, 3);
|
||||
print!("配列の長さを 8 に拡張すると、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 要素を挿入する
|
||||
insert(&mut nums, 6, 3);
|
||||
print!("\nインデックス 3 に数値 6 を挿入すると、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 要素を削除
|
||||
remove(&mut nums, 2);
|
||||
print!("\nインデックス 2 の要素を削除すると、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 配列を走査
|
||||
traverse(&nums);
|
||||
|
||||
// 要素を探索する
|
||||
let index = find(&nums, 3).unwrap();
|
||||
println!("\nnums 内で要素 3 を検索すると、インデックス = {}", index);
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* File: linked_list.rs
|
||||
* Created Time: 2023-03-05
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::{print_util, ListNode};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/* 連結リストでノード n0 の後ろにノード P を挿入する */
|
||||
#[allow(non_snake_case)]
|
||||
pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
|
||||
let n1 = n0.borrow_mut().next.take();
|
||||
P.borrow_mut().next = n1;
|
||||
n0.borrow_mut().next = Some(P);
|
||||
}
|
||||
|
||||
/* 連結リストでノード n0 の直後のノードを削除する */
|
||||
#[allow(non_snake_case)]
|
||||
pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
|
||||
// n0 -> P -> n1
|
||||
let P = n0.borrow_mut().next.take();
|
||||
if let Some(node) = P {
|
||||
let n1 = node.borrow_mut().next.take();
|
||||
n0.borrow_mut().next = n1;
|
||||
}
|
||||
}
|
||||
|
||||
/* 連結リスト内で index 番目のノードにアクセス */
|
||||
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {
|
||||
fn dfs<T>(
|
||||
head: Option<&Rc<RefCell<ListNode<T>>>>,
|
||||
index: i32,
|
||||
) -> Option<Rc<RefCell<ListNode<T>>>> {
|
||||
if index <= 0 {
|
||||
return head.cloned();
|
||||
}
|
||||
|
||||
if let Some(node) = head {
|
||||
dfs(node.borrow().next.as_ref(), index - 1)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
dfs(Some(head).as_ref(), index)
|
||||
}
|
||||
|
||||
/* 連結リストで値が target の最初のノードを探す */
|
||||
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {
|
||||
fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {
|
||||
if let Some(node) = head {
|
||||
if node.borrow().val == target {
|
||||
return idx;
|
||||
}
|
||||
return find(node.borrow().next.as_ref(), target, idx + 1);
|
||||
} else {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
find(Some(head).as_ref(), target, 0)
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* 連結リストを初期化 */
|
||||
// 各ノードを初期化
|
||||
let n0 = ListNode::new(1);
|
||||
let n1 = ListNode::new(3);
|
||||
let n2 = ListNode::new(2);
|
||||
let n3 = ListNode::new(5);
|
||||
let n4 = ListNode::new(4);
|
||||
// ノード間の参照を構築する
|
||||
n0.borrow_mut().next = Some(n1.clone());
|
||||
n1.borrow_mut().next = Some(n2.clone());
|
||||
n2.borrow_mut().next = Some(n3.clone());
|
||||
n3.borrow_mut().next = Some(n4.clone());
|
||||
print!("初期化された連結リストは ");
|
||||
print_util::print_linked_list(&n0);
|
||||
|
||||
/* ノードを挿入 */
|
||||
insert(&n0, ListNode::new(0));
|
||||
print!("ノード挿入後の連結リストは ");
|
||||
print_util::print_linked_list(&n0);
|
||||
|
||||
/* ノードを削除 */
|
||||
remove(&n0);
|
||||
print!("ノード削除後の連結リストは ");
|
||||
print_util::print_linked_list(&n0);
|
||||
|
||||
/* ノードにアクセス */
|
||||
let node = access(n0.clone(), 3);
|
||||
println!("連結リストのインデックス 3 にあるノードの値 = {}", node.unwrap().borrow().val);
|
||||
|
||||
/* ノードを探索 */
|
||||
let index = find(n0.clone(), 2);
|
||||
println!("連結リスト内で値が 2 のノードのインデックス = {}", index);
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* File: list.rs
|
||||
* Created Time: 2023-01-18
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
// リストを初期化
|
||||
let mut nums: Vec<i32> = vec![1, 3, 2, 5, 4];
|
||||
print!("リスト nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 要素にアクセス
|
||||
let num = nums[1];
|
||||
println!("\nインデックス 1 の要素にアクセスすると、num = {num}");
|
||||
|
||||
// 要素を更新
|
||||
nums[1] = 0;
|
||||
print!("インデックス 1 の要素を 0 に更新すると、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// リストを空にする
|
||||
nums.clear();
|
||||
print!("\nリストを空にした後、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 末尾に要素を追加
|
||||
nums.push(1);
|
||||
nums.push(3);
|
||||
nums.push(2);
|
||||
nums.push(5);
|
||||
nums.push(4);
|
||||
print!("\n要素を追加した後、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 中間に要素を挿入
|
||||
nums.insert(3, 6);
|
||||
print!("\nインデックス 3 に数値 6 を挿入すると、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// 要素を削除
|
||||
nums.remove(3);
|
||||
print!("\nインデックス 3 の要素を削除すると、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// インデックスでリストを走査
|
||||
let mut _count = 0;
|
||||
for i in 0..nums.len() {
|
||||
_count += nums[i];
|
||||
}
|
||||
// リスト要素を直接走査
|
||||
_count = 0;
|
||||
for x in &nums {
|
||||
_count += x;
|
||||
}
|
||||
|
||||
// 2 つのリストを連結する
|
||||
let mut nums1 = vec![6, 8, 7, 10, 9];
|
||||
nums.append(&mut nums1); // append(move)の後では nums1 は空になる!
|
||||
|
||||
// nums.extend(&nums1); // extend(借用)の後も nums1 は引き続き使える
|
||||
print!("\nリスト nums1 を nums の後ろに連結すると、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
|
||||
// リストをソート
|
||||
nums.sort();
|
||||
print!("\nリストをソートした後、nums = ");
|
||||
print_util::print_array(&nums);
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* File: my_list.rs
|
||||
* Created Time: 2023-03-11
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
use hello_algo_rust::include::print_util;
|
||||
|
||||
/* リストクラス */
|
||||
#[allow(dead_code)]
|
||||
struct MyList {
|
||||
arr: Vec<i32>, // 配列(リスト要素を格納)
|
||||
capacity: usize, // リスト容量
|
||||
size: usize, // リストの長さ(現在の要素数)
|
||||
extend_ratio: usize, // リスト拡張時の増加倍率
|
||||
}
|
||||
|
||||
#[allow(unused, unused_comparisons)]
|
||||
impl MyList {
|
||||
/* コンストラクタ */
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
let mut vec = vec![0; capacity];
|
||||
Self {
|
||||
arr: vec,
|
||||
capacity,
|
||||
size: 0,
|
||||
extend_ratio: 2,
|
||||
}
|
||||
}
|
||||
|
||||
/* リストの長さを取得(現在の要素数) */
|
||||
pub fn size(&self) -> usize {
|
||||
return self.size;
|
||||
}
|
||||
|
||||
/* リスト容量を取得する */
|
||||
pub fn capacity(&self) -> usize {
|
||||
return self.capacity;
|
||||
}
|
||||
|
||||
/* 要素にアクセス */
|
||||
pub fn get(&self, index: usize) -> i32 {
|
||||
// インデックスが範囲外なら例外を送出する。以下同様
|
||||
if index >= self.size {
|
||||
panic!("インデックスが範囲外です")
|
||||
};
|
||||
return self.arr[index];
|
||||
}
|
||||
|
||||
/* 要素を更新 */
|
||||
pub fn set(&mut self, index: usize, num: i32) {
|
||||
if index >= self.size {
|
||||
panic!("インデックスが範囲外です")
|
||||
};
|
||||
self.arr[index] = num;
|
||||
}
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
pub fn add(&mut self, num: i32) {
|
||||
// 要素数が容量を超えると、拡張機構が発動する
|
||||
if self.size == self.capacity() {
|
||||
self.extend_capacity();
|
||||
}
|
||||
self.arr[self.size] = num;
|
||||
// 要素数を更新
|
||||
self.size += 1;
|
||||
}
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
pub fn insert(&mut self, index: usize, num: i32) {
|
||||
if index >= self.size() {
|
||||
panic!("インデックスが範囲外です")
|
||||
};
|
||||
// 要素数が容量を超えると、拡張機構が発動する
|
||||
if self.size == self.capacity() {
|
||||
self.extend_capacity();
|
||||
}
|
||||
// index 以降の要素をすべて 1 つ後ろへずらす
|
||||
for j in (index..self.size).rev() {
|
||||
self.arr[j + 1] = self.arr[j];
|
||||
}
|
||||
self.arr[index] = num;
|
||||
// 要素数を更新
|
||||
self.size += 1;
|
||||
}
|
||||
|
||||
/* 要素を削除 */
|
||||
pub fn remove(&mut self, index: usize) -> i32 {
|
||||
if index >= self.size() {
|
||||
panic!("インデックスが範囲外です")
|
||||
};
|
||||
let num = self.arr[index];
|
||||
// インデックス index より後の要素をすべて 1 つ前に移動する
|
||||
for j in index..self.size - 1 {
|
||||
self.arr[j] = self.arr[j + 1];
|
||||
}
|
||||
// 要素数を更新
|
||||
self.size -= 1;
|
||||
// 削除された要素を返す
|
||||
return num;
|
||||
}
|
||||
|
||||
/* リストの拡張 */
|
||||
pub fn extend_capacity(&mut self) {
|
||||
// 元の配列の extend_ratio 倍の長さを持つ新しい配列を作成し、元の配列をコピーする
|
||||
let new_capacity = self.capacity * self.extend_ratio;
|
||||
self.arr.resize(new_capacity, 0);
|
||||
// リストの容量を更新
|
||||
self.capacity = new_capacity;
|
||||
}
|
||||
|
||||
/* リストを配列に変換する */
|
||||
pub fn to_array(&self) -> Vec<i32> {
|
||||
// 有効長の範囲内のリスト要素のみを変換
|
||||
let mut arr = Vec::new();
|
||||
for i in 0..self.size {
|
||||
arr.push(self.get(i));
|
||||
}
|
||||
arr
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fn main() {
|
||||
/* リストを初期化 */
|
||||
let mut nums = MyList::new(10);
|
||||
/* 末尾に要素を追加 */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
print!("リスト nums = ");
|
||||
print_util::print_array(&nums.to_array());
|
||||
print!(" 、容量 = {} 、長さ = {}", nums.capacity(), nums.size());
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums.insert(3, 6);
|
||||
print!("\nインデックス 3 に数値 6 を挿入すると、nums = ");
|
||||
print_util::print_array(&nums.to_array());
|
||||
|
||||
/* 要素を削除 */
|
||||
nums.remove(3);
|
||||
print!("\nインデックス 3 の要素を削除すると、nums = ");
|
||||
print_util::print_array(&nums.to_array());
|
||||
|
||||
/* 要素にアクセス */
|
||||
let num = nums.get(1);
|
||||
println!("\nインデックス 1 の要素にアクセスすると、num = {num}");
|
||||
|
||||
/* 要素を更新 */
|
||||
nums.set(1, 0);
|
||||
print!("インデックス 1 の要素を 0 に更新すると、nums = ");
|
||||
print_util::print_array(&nums.to_array());
|
||||
|
||||
/* 拡張機構をテストする */
|
||||
for i in 0..10 {
|
||||
// i = 5 のとき、リスト長が容量を超えるため、この時点で拡張機構が発動する
|
||||
nums.add(i);
|
||||
}
|
||||
print!("\n拡張後のリスト nums = ");
|
||||
print_util::print_array(&nums.to_array());
|
||||
print!(" 、容量 = {} 、長さ = {}", nums.capacity(), nums.size());
|
||||
}
|
||||
Reference in New Issue
Block a user