mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-05 12:14:20 +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,107 @@
|
||||
// File: array.cs
|
||||
// Created Time: 2022-12-14
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class array {
|
||||
/* 要素へランダムアクセス */
|
||||
int RandomAccess(int[] nums) {
|
||||
Random random = new();
|
||||
// 区間 [0, nums.Length) からランダムに数字を 1 つ選ぶ
|
||||
int randomIndex = random.Next(nums.Length);
|
||||
// ランダムな要素を取得して返す
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* 配列長を拡張する */
|
||||
int[] Extend(int[] nums, int enlarge) {
|
||||
// 拡張後の長さを持つ配列を初期化する
|
||||
int[] res = new int[nums.Length + enlarge];
|
||||
// 元の配列の全要素を新しい配列にコピー
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// 拡張後の新しい配列を返す
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 配列の index 番目に要素 num を挿入 */
|
||||
void Insert(int[] nums, int num, int index) {
|
||||
// インデックス index 以降の全要素を 1 つ後ろへ移動する
|
||||
for (int i = nums.Length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// index の要素に num を代入する
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* index の要素を削除する */
|
||||
void Remove(int[] nums, int index) {
|
||||
// インデックス index より後ろの全要素を 1 つ前へ移動する
|
||||
for (int i = index; i < nums.Length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 配列を走査 */
|
||||
void Traverse(int[] nums) {
|
||||
int count = 0;
|
||||
// インデックスで配列を走査
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// 配列要素を直接走査
|
||||
foreach (int num in nums) {
|
||||
count += num;
|
||||
}
|
||||
}
|
||||
|
||||
/* 配列内で指定要素を探す */
|
||||
int Find(int[] nums, int target) {
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
if (nums[i] == target)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 補助関数:配列を文字列に変換 */
|
||||
string ToString(int[] nums) {
|
||||
return string.Join(",", nums);
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// 配列を初期化
|
||||
int[] arr = new int[5];
|
||||
Console.WriteLine("配列 arr = " + ToString(arr));
|
||||
int[] nums = [1, 3, 2, 5, 4];
|
||||
Console.WriteLine("配列 nums = " + ToString(nums));
|
||||
|
||||
// ランダムアクセス
|
||||
int randomNum = RandomAccess(nums);
|
||||
Console.WriteLine("nums からランダムな要素を取得 " + randomNum);
|
||||
|
||||
// 長さを拡張
|
||||
nums = Extend(nums, 3);
|
||||
Console.WriteLine("配列の長さを 8 まで拡張すると nums = " + ToString(nums));
|
||||
|
||||
// 要素を挿入する
|
||||
Insert(nums, 6, 3);
|
||||
Console.WriteLine("インデックス 3 に数値 6 を挿入すると nums = " + ToString(nums));
|
||||
|
||||
// 要素を削除
|
||||
Remove(nums, 2);
|
||||
Console.WriteLine("インデックス 2 の要素を削除すると nums = " + ToString(nums));
|
||||
|
||||
// 配列を走査
|
||||
Traverse(nums);
|
||||
|
||||
// 要素を探索する
|
||||
int index = Find(nums, 3);
|
||||
Console.WriteLine("nums 内で要素 3 を検索するとインデックス = " + index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// File: linked_list.cs
|
||||
// Created Time: 2022-12-16
|
||||
// Author: mingXta (1195669834@qq.com)
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class linked_list {
|
||||
/* 連結リストでノード n0 の後ろにノード P を挿入する */
|
||||
void Insert(ListNode n0, ListNode P) {
|
||||
ListNode? n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
}
|
||||
|
||||
/* 連結リストでノード n0 の直後のノードを削除する */
|
||||
void Remove(ListNode n0) {
|
||||
if (n0.next == null)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next;
|
||||
ListNode? n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* 連結リスト内で index 番目のノードにアクセス */
|
||||
ListNode? Access(ListNode? head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == null)
|
||||
return null;
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 連結リストで値が target の最初のノードを探す */
|
||||
int Find(ListNode? head, int target) {
|
||||
int index = 0;
|
||||
while (head != null) {
|
||||
if (head.val == target)
|
||||
return index;
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
// 連結リストを初期化する
|
||||
// 各ノードを初期化する
|
||||
ListNode n0 = new(1);
|
||||
ListNode n1 = new(3);
|
||||
ListNode n2 = new(2);
|
||||
ListNode n3 = new(5);
|
||||
ListNode n4 = new(4);
|
||||
// ノード間の参照を構築する
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
Console.WriteLine($"初期化した連結リストは{n0}");
|
||||
|
||||
// ノードを挿入
|
||||
Insert(n0, new ListNode(0));
|
||||
Console.WriteLine($"ノード挿入後の連結リストは{n0}");
|
||||
|
||||
// ノードを削除
|
||||
Remove(n0);
|
||||
Console.WriteLine($"ノード削除後の連結リストは{n0}");
|
||||
|
||||
// ノードにアクセス
|
||||
ListNode? node = Access(n0, 3);
|
||||
Console.WriteLine($"連結リストのインデックス 3 にあるノードの値 = {node?.val}");
|
||||
|
||||
// ノードを探索
|
||||
int index = Find(n0, 2);
|
||||
Console.WriteLine($"連結リスト内で値が 2 のノードのインデックス = {index}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* File: list.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
public class list {
|
||||
[Test]
|
||||
public void Test() {
|
||||
|
||||
/* リストを初期化 */
|
||||
int[] numbers = [1, 3, 2, 5, 4];
|
||||
List<int> nums = [.. numbers];
|
||||
Console.WriteLine("リスト nums = " + string.Join(",", nums));
|
||||
|
||||
/* 要素にアクセス */
|
||||
int num = nums[1];
|
||||
Console.WriteLine("インデックス 1 の要素にアクセスすると num = " + num);
|
||||
|
||||
/* 要素を更新 */
|
||||
nums[1] = 0;
|
||||
Console.WriteLine("インデックス 1 の要素を 0 に更新すると nums = " + string.Join(",", nums));
|
||||
|
||||
/* リストを空にする */
|
||||
nums.Clear();
|
||||
Console.WriteLine("リストを空にした後 nums = " + string.Join(",", nums));
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
nums.Add(1);
|
||||
nums.Add(3);
|
||||
nums.Add(2);
|
||||
nums.Add(5);
|
||||
nums.Add(4);
|
||||
Console.WriteLine("要素を追加した後 nums = " + string.Join(",", nums));
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums.Insert(3, 6);
|
||||
Console.WriteLine("インデックス 3 に数値 6 を挿入すると nums = " + string.Join(",", nums));
|
||||
|
||||
/* 要素を削除 */
|
||||
nums.RemoveAt(3);
|
||||
Console.WriteLine("インデックス 3 の要素を削除すると nums = " + string.Join(",", nums));
|
||||
|
||||
/* インデックスでリストを走査 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.Count; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* リスト要素を直接走査 */
|
||||
count = 0;
|
||||
foreach (int x in nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 2 つのリストを連結する */
|
||||
List<int> nums1 = [6, 8, 7, 10, 9];
|
||||
nums.AddRange(nums1);
|
||||
Console.WriteLine("リスト nums1 を nums の後ろに連結すると nums = " + string.Join(",", nums));
|
||||
|
||||
/* リストをソート */
|
||||
nums.Sort(); // ソート後、リスト要素は小さい順に並ぶ
|
||||
Console.WriteLine("リストをソートした後 nums = " + string.Join(",", nums));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* File: my_list.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_array_and_linkedlist;
|
||||
|
||||
/* リストクラス */
|
||||
class MyList {
|
||||
private int[] arr; // 配列(リスト要素を格納)
|
||||
private int arrCapacity = 10; // リスト容量
|
||||
private int arrSize = 0; // リストの長さ(現在の要素数)
|
||||
private readonly int extendRatio = 2; // リスト拡張時の増加倍率
|
||||
|
||||
/* コンストラクタ */
|
||||
public MyList() {
|
||||
arr = new int[arrCapacity];
|
||||
}
|
||||
|
||||
/* リストの長さを取得(現在の要素数) */
|
||||
public int Size() {
|
||||
return arrSize;
|
||||
}
|
||||
|
||||
/* リスト容量を取得する */
|
||||
public int Capacity() {
|
||||
return arrCapacity;
|
||||
}
|
||||
|
||||
/* 要素にアクセス */
|
||||
public int Get(int index) {
|
||||
// インデックスが範囲外なら例外を送出する。以下同様
|
||||
if (index < 0 || index >= arrSize)
|
||||
throw new IndexOutOfRangeException("インデックスが範囲外です");
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* 要素を更新 */
|
||||
public void Set(int index, int num) {
|
||||
if (index < 0 || index >= arrSize)
|
||||
throw new IndexOutOfRangeException("インデックスが範囲外です");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
public void Add(int num) {
|
||||
// 要素数が容量を超えると、拡張機構が発動する
|
||||
if (arrSize == arrCapacity)
|
||||
ExtendCapacity();
|
||||
arr[arrSize] = num;
|
||||
// 要素数を更新
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
public void Insert(int index, int num) {
|
||||
if (index < 0 || index >= arrSize)
|
||||
throw new IndexOutOfRangeException("インデックスが範囲外です");
|
||||
// 要素数が容量を超えると、拡張機構が発動する
|
||||
if (arrSize == arrCapacity)
|
||||
ExtendCapacity();
|
||||
// index 以降の要素をすべて 1 つ後ろへずらす
|
||||
for (int j = arrSize - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
arr[index] = num;
|
||||
// 要素数を更新
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 要素を削除 */
|
||||
public int Remove(int index) {
|
||||
if (index < 0 || index >= arrSize)
|
||||
throw new IndexOutOfRangeException("インデックスが範囲外です");
|
||||
int num = arr[index];
|
||||
// インデックス index より後の要素をすべて 1 つ前に移動する
|
||||
for (int j = index; j < arrSize - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
// 要素数を更新
|
||||
arrSize--;
|
||||
// 削除された要素を返す
|
||||
return num;
|
||||
}
|
||||
|
||||
/* リストの拡張 */
|
||||
public void ExtendCapacity() {
|
||||
// `arrCapacity * extendRatio` の長さを持つ配列を新規作成し、元の配列を新しい配列にコピーする
|
||||
Array.Resize(ref arr, arrCapacity * extendRatio);
|
||||
// リストの容量を更新
|
||||
arrCapacity = arr.Length;
|
||||
}
|
||||
|
||||
/* リストを配列に変換する */
|
||||
public int[] ToArray() {
|
||||
// 有効長の範囲内のリスト要素のみを変換
|
||||
int[] arr = new int[arrSize];
|
||||
for (int i = 0; i < arrSize; i++) {
|
||||
arr[i] = Get(i);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
public class my_list {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* リストを初期化 */
|
||||
MyList nums = new();
|
||||
/* 末尾に要素を追加 */
|
||||
nums.Add(1);
|
||||
nums.Add(3);
|
||||
nums.Add(2);
|
||||
nums.Add(5);
|
||||
nums.Add(4);
|
||||
Console.WriteLine("リスト nums = " + string.Join(",", nums.ToArray()) +
|
||||
" ,容量 = " + nums.Capacity() + " ,長さ = " + nums.Size());
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums.Insert(3, 6);
|
||||
Console.WriteLine("インデックス 3 に数値 6 を挿入すると nums = " + string.Join(",", nums.ToArray()));
|
||||
|
||||
/* 要素を削除 */
|
||||
nums.Remove(3);
|
||||
Console.WriteLine("インデックス 3 の要素を削除すると nums = " + string.Join(",", nums.ToArray()));
|
||||
|
||||
/* 要素にアクセス */
|
||||
int num = nums.Get(1);
|
||||
Console.WriteLine("インデックス 1 の要素にアクセスすると num = " + num);
|
||||
|
||||
/* 要素を更新 */
|
||||
nums.Set(1, 0);
|
||||
Console.WriteLine("インデックス 1 の要素を 0 に更新すると nums = " + string.Join(",", nums.ToArray()));
|
||||
|
||||
/* 拡張機構をテストする */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// i = 5 のとき、リスト長が容量を超えるため、この時点で拡張機構が発動する
|
||||
nums.Add(i);
|
||||
}
|
||||
Console.WriteLine("拡張後のリスト nums = " + string.Join(",", nums.ToArray()) +
|
||||
" ,容量 = " + nums.Capacity() + " ,長さ = " + nums.Size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user