mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-30 17:44:26 +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,4 @@
|
||||
add_executable(array array.cpp)
|
||||
add_executable(linked_list linked_list.cpp)
|
||||
add_executable(list list.cpp)
|
||||
add_executable(my_list my_list.cpp)
|
||||
@@ -6,57 +6,57 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 要素への乱数アクセス */
|
||||
/* 要素へランダムアクセス */
|
||||
int randomAccess(int *nums, int size) {
|
||||
// [0, size)の範囲で乱数を選択
|
||||
// 区間 [0, size) からランダムに 1 つの数を選ぶ
|
||||
int randomIndex = rand() % size;
|
||||
// 乱数要素を取得して返却
|
||||
// ランダムな要素を取得して返す
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* 配列長の拡張 */
|
||||
/* 配列長を拡張する */
|
||||
int *extend(int *nums, int size, int enlarge) {
|
||||
// 拡張された長さの配列を初期化
|
||||
// 拡張後の長さを持つ配列を初期化する
|
||||
int *res = new int[size + enlarge];
|
||||
// 元の配列の全要素を新しい配列にコピー
|
||||
for (int i = 0; i < size; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// メモリを解放
|
||||
// メモリを解放する
|
||||
delete[] nums;
|
||||
// 拡張後の新しい配列を返却
|
||||
// 拡張後の新しい配列を返す
|
||||
return res;
|
||||
}
|
||||
|
||||
/* `index`に要素numを挿入 */
|
||||
/* 配列の index 番目に要素 num を挿入 */
|
||||
void insert(int *nums, int size, int num, int index) {
|
||||
// `index`より後のすべての要素を1つ後ろに移動
|
||||
// インデックス index 以降の全要素を 1 つ後ろへ移動する
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// indexの位置にnumを代入
|
||||
// index の要素に num を代入する
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* `index`の要素を削除 */
|
||||
/* index の要素を削除する */
|
||||
void remove(int *nums, int size, int index) {
|
||||
// `index`より後のすべての要素を1つ前に移動
|
||||
// インデックス index より後ろの全要素を 1 つ前へ移動する
|
||||
for (int i = index; i < size - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* 配列の走査 */
|
||||
/* 配列を走査 */
|
||||
void traverse(int *nums, int size) {
|
||||
int count = 0;
|
||||
// インデックスによる配列の走査
|
||||
// インデックスで配列を走査
|
||||
for (int i = 0; i < size; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* 配列内の指定要素を検索 */
|
||||
/* 配列内で指定要素を探す */
|
||||
int find(int *nums, int size, int target) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (nums[i] == target)
|
||||
@@ -65,49 +65,49 @@ int find(int *nums, int size, int target) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 配列を初期化 */
|
||||
int size = 5;
|
||||
int *arr = new int[size];
|
||||
cout << "Array arr = ";
|
||||
cout << "配列 arr = ";
|
||||
printArray(arr, size);
|
||||
|
||||
int *nums = new int[size]{1, 3, 2, 5, 4};
|
||||
cout << "Array nums = ";
|
||||
cout << "配列 nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 乱数アクセス */
|
||||
/* ランダムアクセス */
|
||||
int randomNum = randomAccess(nums, size);
|
||||
cout << "Get a random element from nums = " << randomNum << endl;
|
||||
cout << "nums から取得したランダム要素 " << randomNum << endl;
|
||||
|
||||
/* 長さの拡張 */
|
||||
/* 長さを拡張 */
|
||||
int enlarge = 3;
|
||||
nums = extend(nums, size, enlarge);
|
||||
size += enlarge;
|
||||
cout << "Extend the array length to 8, resulting in nums = ";
|
||||
cout << "配列長を 8 に拡張し、nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 要素の挿入 */
|
||||
/* 要素を挿入する */
|
||||
insert(nums, size, 6, 3);
|
||||
cout << "Insert the number 6 at index 3, resulting in nums = ";
|
||||
cout << "インデックス 3 に数値 6 を挿入し、nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 要素の削除 */
|
||||
/* 要素を削除 */
|
||||
remove(nums, size, 2);
|
||||
cout << "Remove the element at index 2, resulting in nums = ";
|
||||
cout << "インデックス 2 の要素を削除し、nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 配列の走査 */
|
||||
/* 配列を走査 */
|
||||
traverse(nums, size);
|
||||
|
||||
/* 要素の検索 */
|
||||
/* 要素を探索する */
|
||||
int index = find(nums, size, 3);
|
||||
cout << "Find element 3 in nums, index = " << index << endl;
|
||||
cout << "nums 内で要素 3 を検索し、インデックス = " << index << endl;
|
||||
|
||||
// メモリを解放
|
||||
// メモリを解放する
|
||||
delete[] arr;
|
||||
delete[] nums;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 連結リストのノードn0の後にノードPを挿入 */
|
||||
/* 連結リストでノード n0 の後ろにノード P を挿入する */
|
||||
void insert(ListNode *n0, ListNode *P) {
|
||||
ListNode *n1 = n0->next;
|
||||
P->next = n1;
|
||||
n0->next = P;
|
||||
}
|
||||
|
||||
/* 連結リストのノードn0の後の最初のノードを削除 */
|
||||
/* 連結リストでノード n0 の直後のノードを削除する */
|
||||
void remove(ListNode *n0) {
|
||||
if (n0->next == nullptr)
|
||||
return;
|
||||
@@ -21,11 +21,11 @@ void remove(ListNode *n0) {
|
||||
ListNode *P = n0->next;
|
||||
ListNode *n1 = P->next;
|
||||
n0->next = n1;
|
||||
// メモリを解放
|
||||
// メモリを解放する
|
||||
delete P;
|
||||
}
|
||||
|
||||
/* 連結リストの`index`番目のノードにアクセス */
|
||||
/* 連結リスト内で index 番目のノードにアクセス */
|
||||
ListNode *access(ListNode *head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == nullptr)
|
||||
@@ -35,7 +35,7 @@ ListNode *access(ListNode *head, int index) {
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 連結リストで値がtargetの最初のノードを検索 */
|
||||
/* 連結リストで値が target の最初のノードを探す */
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head != nullptr) {
|
||||
@@ -47,7 +47,7 @@ int find(ListNode *head, int target) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 連結リストを初期化 */
|
||||
// 各ノードを初期化
|
||||
@@ -56,34 +56,34 @@ int main() {
|
||||
ListNode *n2 = new ListNode(2);
|
||||
ListNode *n3 = new ListNode(5);
|
||||
ListNode *n4 = new ListNode(4);
|
||||
// ノード間の参照を構築
|
||||
// ノード間の参照を構築する
|
||||
n0->next = n1;
|
||||
n1->next = n2;
|
||||
n2->next = n3;
|
||||
n3->next = n4;
|
||||
cout << "The initialized linked list is" << endl;
|
||||
cout << "初期化した連結リストは" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* ノードを挿入 */
|
||||
insert(n0, new ListNode(0));
|
||||
cout << "Linked list after inserting the node is" << endl;
|
||||
cout << "ノード挿入後の連結リストは" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* ノードを削除 */
|
||||
remove(n0);
|
||||
cout << "Linked list after removing the node is" << endl;
|
||||
cout << "ノード削除後の連結リストは" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* ノードにアクセス */
|
||||
ListNode *node = access(n0, 3);
|
||||
cout << "The value of the node at index 3 in the linked list = " << node->val << endl;
|
||||
cout << "連結リストのインデックス 3 のノードの値 = " << node->val << endl;
|
||||
|
||||
/* ノードを検索 */
|
||||
/* ノードを探索 */
|
||||
int index = find(n0, 2);
|
||||
cout << "The index of the node with value 2 in the linked list = " << index << endl;
|
||||
cout << "連結リスト内で値が 2 のノードのインデックス = " << index << endl;
|
||||
|
||||
// メモリを解放
|
||||
// メモリを解放する
|
||||
freeMemoryLinkedList(n0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,25 +6,25 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* リストを初期化 */
|
||||
vector<int> nums = {1, 3, 2, 5, 4};
|
||||
cout << "List nums = ";
|
||||
cout << "リスト nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 要素にアクセス */
|
||||
int num = nums[1];
|
||||
cout << "Access the element at index 1, obtained num = " << num << endl;
|
||||
cout << "インデックス 1 の要素にアクセスすると、num = " << num << endl;
|
||||
|
||||
/* 要素を更新 */
|
||||
nums[1] = 0;
|
||||
cout << "Update the element at index 1 to 0, resulting in nums = ";
|
||||
cout << "インデックス 1 の要素を 0 に更新すると、nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* リストをクリア */
|
||||
/* リストを空にする */
|
||||
nums.clear();
|
||||
cout << "After clearing the list, nums = ";
|
||||
cout << "リストを空にした後の nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
@@ -33,40 +33,40 @@ int main() {
|
||||
nums.push_back(2);
|
||||
nums.push_back(5);
|
||||
nums.push_back(4);
|
||||
cout << "After adding elements, nums = ";
|
||||
cout << "要素追加後の nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums.insert(nums.begin() + 3, 6);
|
||||
cout << "Insert the number 6 at index 3, resulting in nums = ";
|
||||
cout << "インデックス 3 に数値 6 を挿入し、nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 要素を削除 */
|
||||
nums.erase(nums.begin() + 3);
|
||||
cout << "Remove the element at index 3, resulting in nums = ";
|
||||
cout << "インデックス 3 の要素を削除すると、nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* インデックスによるリストの走査 */
|
||||
/* インデックスでリストを走査 */
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* リスト要素の走査 */
|
||||
/* リスト要素を直接走査 */
|
||||
count = 0;
|
||||
for (int x : nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* 2つのリストを連結 */
|
||||
/* 2 つのリストを連結する */
|
||||
vector<int> nums1 = {6, 8, 7, 10, 9};
|
||||
nums.insert(nums.end(), nums1.begin(), nums1.end());
|
||||
cout << "Concatenate list nums1 to nums, resulting in nums = ";
|
||||
cout << "リスト nums1 を nums の後ろに連結すると、nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* リストをソート */
|
||||
sort(nums.begin(), nums.end());
|
||||
cout << "After sorting the list, nums = ";
|
||||
cout << "リストをソートした後の nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
class MyList {
|
||||
private:
|
||||
int *arr; // 配列(リスト要素を格納)
|
||||
int arrCapacity = 10; // リストの容量
|
||||
int arrCapacity = 10; // リスト容量
|
||||
int arrSize = 0; // リストの長さ(現在の要素数)
|
||||
int extendRatio = 2; // リスト拡張時の倍率
|
||||
int extendRatio = 2; // リスト拡張時の増加倍率
|
||||
|
||||
public:
|
||||
/* コンストラクタ */
|
||||
@@ -20,39 +20,39 @@ class MyList {
|
||||
arr = new int[arrCapacity];
|
||||
}
|
||||
|
||||
/* デストラクタ */
|
||||
/* デストラクタメソッド */
|
||||
~MyList() {
|
||||
delete[] arr;
|
||||
}
|
||||
|
||||
/* リストの長さを取得(現在の要素数)*/
|
||||
/* リストの長さを取得(現在の要素数) */
|
||||
int size() {
|
||||
return arrSize;
|
||||
}
|
||||
|
||||
/* リストの容量を取得 */
|
||||
/* リスト容量を取得する */
|
||||
int capacity() {
|
||||
return arrCapacity;
|
||||
}
|
||||
|
||||
/* 要素にアクセス */
|
||||
int get(int index) {
|
||||
// インデックスが範囲外の場合、例外をスロー(以下同様)
|
||||
// インデックスが範囲外なら例外を送出する。以下同様
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
throw out_of_range("インデックスが範囲外");
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* 要素を更新 */
|
||||
void set(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
throw out_of_range("インデックスが範囲外");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
void add(int num) {
|
||||
// 要素数が容量を超えた場合、拡張メカニズムをトリガー
|
||||
// 要素数が容量を超えると、拡張機構が発動する
|
||||
if (size() == capacity())
|
||||
extendCapacity();
|
||||
arr[size()] = num;
|
||||
@@ -63,11 +63,11 @@ class MyList {
|
||||
/* 中間に要素を挿入 */
|
||||
void insert(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
// 要素数が容量を超えた場合、拡張メカニズムをトリガー
|
||||
throw out_of_range("インデックスが範囲外");
|
||||
// 要素数が容量を超えると、拡張機構が発動する
|
||||
if (size() == capacity())
|
||||
extendCapacity();
|
||||
// `index`より後のすべての要素を1つ後ろに移動
|
||||
// index 以降の要素をすべて 1 つ後ろへずらす
|
||||
for (int j = size() - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
@@ -79,36 +79,36 @@ class MyList {
|
||||
/* 要素を削除 */
|
||||
int remove(int index) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
throw out_of_range("インデックスが範囲外");
|
||||
int num = arr[index];
|
||||
// `index`より後のすべての要素を1つ前に移動
|
||||
// インデックス index より後の要素をすべて 1 つ前に移動する
|
||||
for (int j = index; j < size() - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
// 要素数を更新
|
||||
arrSize--;
|
||||
// 削除された要素を返却
|
||||
// 削除された要素を返す
|
||||
return num;
|
||||
}
|
||||
|
||||
/* リストを拡張 */
|
||||
/* リストの拡張 */
|
||||
void extendCapacity() {
|
||||
// 元の配列のextendRatio倍の長さで新しい配列を作成
|
||||
// 元の配列の `extendRatio` 倍の長さを持つ新しい配列を作成する
|
||||
int newCapacity = capacity() * extendRatio;
|
||||
int *tmp = arr;
|
||||
arr = new int[newCapacity];
|
||||
// 元の配列のすべての要素を新しい配列にコピー
|
||||
// 元の配列の全要素を新しい配列にコピー
|
||||
for (int i = 0; i < size(); i++) {
|
||||
arr[i] = tmp[i];
|
||||
}
|
||||
// メモリを解放
|
||||
// メモリを解放する
|
||||
delete[] tmp;
|
||||
arrCapacity = newCapacity;
|
||||
}
|
||||
|
||||
/* リストをVectorに変換して印刷用に使用 */
|
||||
/* 出力用にリストを Vector に変換 */
|
||||
vector<int> toVector() {
|
||||
// 有効な長さ範囲内の要素のみを変換
|
||||
// 有効長の範囲内のリスト要素のみを変換
|
||||
vector<int> vec(size());
|
||||
for (int i = 0; i < size(); i++) {
|
||||
vec[i] = arr[i];
|
||||
@@ -117,7 +117,7 @@ class MyList {
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* リストを初期化 */
|
||||
MyList *nums = new MyList();
|
||||
@@ -127,45 +127,45 @@ int main() {
|
||||
nums->add(2);
|
||||
nums->add(5);
|
||||
nums->add(4);
|
||||
cout << "List nums = ";
|
||||
cout << "リスト nums = ";
|
||||
vector<int> vec = nums->toVector();
|
||||
printVector(vec);
|
||||
cout << "Capacity = " << nums->capacity() << ", length = " << nums->size() << endl;
|
||||
cout << "容量 = " << nums->capacity() << " ,長さ = " << nums->size() << endl;
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums->insert(3, 6);
|
||||
cout << "Insert the number 6 at index 3, resulting in nums = ";
|
||||
cout << "インデックス 3 に数値 6 を挿入し、nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 要素を削除 */
|
||||
nums->remove(3);
|
||||
cout << "Remove the element at index 3, resulting in nums = ";
|
||||
cout << "インデックス 3 の要素を削除すると、nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 要素にアクセス */
|
||||
int num = nums->get(1);
|
||||
cout << "Access the element at index 1, obtained num = " << num << endl;
|
||||
cout << "インデックス 1 の要素にアクセスすると、num = " << num << endl;
|
||||
|
||||
/* 要素を更新 */
|
||||
nums->set(1, 0);
|
||||
cout << "Update the element at index 1 to 0, resulting in nums = ";
|
||||
cout << "インデックス 1 の要素を 0 に更新すると、nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 拡張メカニズムをテスト */
|
||||
/* 拡張機構をテストする */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// i = 5の時、リストの長さがリストの容量を超え、この時点で拡張メカニズムがトリガーされる
|
||||
// i = 5 のとき、リスト長が容量を超えるため、この時点で拡張機構が発動する
|
||||
nums->add(i);
|
||||
}
|
||||
cout << "After extending, list nums = ";
|
||||
cout << "拡張後のリスト nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
cout << "Capacity = " << nums->capacity() << ", length = " << nums->size() << endl;
|
||||
cout << "容量 = " << nums->capacity() << " ,長さ = " << nums->size() << endl;
|
||||
|
||||
// メモリを解放
|
||||
// メモリを解放する
|
||||
delete nums;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user