mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-02 02:24:24 +00:00
docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* File: array.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 要素への乱数アクセス */
|
||||
int randomAccess(int *nums, int size) {
|
||||
// [0, size)の範囲で乱数を選択
|
||||
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を挿入 */
|
||||
void insert(int *nums, int size, int num, int index) {
|
||||
// `index`より後のすべての要素を1つ後ろに移動
|
||||
for (int i = size - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// indexの位置にnumを代入
|
||||
nums[index] = num;
|
||||
}
|
||||
|
||||
/* `index`の要素を削除 */
|
||||
void remove(int *nums, int size, int index) {
|
||||
// `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)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* 配列を初期化 */
|
||||
int size = 5;
|
||||
int *arr = new int[size];
|
||||
cout << "Array arr = ";
|
||||
printArray(arr, size);
|
||||
|
||||
int *nums = new int[size]{1, 3, 2, 5, 4};
|
||||
cout << "Array nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 乱数アクセス */
|
||||
int randomNum = randomAccess(nums, size);
|
||||
cout << "Get a random element from nums = " << randomNum << endl;
|
||||
|
||||
/* 長さの拡張 */
|
||||
int enlarge = 3;
|
||||
nums = extend(nums, size, enlarge);
|
||||
size += enlarge;
|
||||
cout << "Extend the array length to 8, resulting in nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 要素の挿入 */
|
||||
insert(nums, size, 6, 3);
|
||||
cout << "Insert the number 6 at index 3, resulting in nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 要素の削除 */
|
||||
remove(nums, size, 2);
|
||||
cout << "Remove the element at index 2, resulting in nums = ";
|
||||
printArray(nums, size);
|
||||
|
||||
/* 配列の走査 */
|
||||
traverse(nums, size);
|
||||
|
||||
/* 要素の検索 */
|
||||
int index = find(nums, size, 3);
|
||||
cout << "Find element 3 in nums, index = " << index << endl;
|
||||
|
||||
// メモリを解放
|
||||
delete[] arr;
|
||||
delete[] nums;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* File: linked_list.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 連結リストのノード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 == nullptr)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode *P = n0->next;
|
||||
ListNode *n1 = P->next;
|
||||
n0->next = n1;
|
||||
// メモリを解放
|
||||
delete P;
|
||||
}
|
||||
|
||||
/* 連結リストの`index`番目のノードにアクセス */
|
||||
ListNode *access(ListNode *head, int index) {
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (head == nullptr)
|
||||
return nullptr;
|
||||
head = head->next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* 連結リストで値がtargetの最初のノードを検索 */
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head != nullptr) {
|
||||
if (head->val == target)
|
||||
return index;
|
||||
head = head->next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* 連結リストを初期化 */
|
||||
// 各ノードを初期化
|
||||
ListNode *n0 = new ListNode(1);
|
||||
ListNode *n1 = new ListNode(3);
|
||||
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;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* ノードを挿入 */
|
||||
insert(n0, new ListNode(0));
|
||||
cout << "Linked list after inserting the node is" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* ノードを削除 */
|
||||
remove(n0);
|
||||
cout << "Linked list after removing the node is" << endl;
|
||||
printLinkedList(n0);
|
||||
|
||||
/* ノードにアクセス */
|
||||
ListNode *node = access(n0, 3);
|
||||
cout << "The value of the node at index 3 in the linked list = " << node->val << endl;
|
||||
|
||||
/* ノードを検索 */
|
||||
int index = find(n0, 2);
|
||||
cout << "The index of the node with value 2 in the linked list = " << index << endl;
|
||||
|
||||
// メモリを解放
|
||||
freeMemoryLinkedList(n0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* File: list.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* リストを初期化 */
|
||||
vector<int> nums = {1, 3, 2, 5, 4};
|
||||
cout << "List nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 要素にアクセス */
|
||||
int num = nums[1];
|
||||
cout << "Access the element at index 1, obtained num = " << num << endl;
|
||||
|
||||
/* 要素を更新 */
|
||||
nums[1] = 0;
|
||||
cout << "Update the element at index 1 to 0, resulting in nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* リストをクリア */
|
||||
nums.clear();
|
||||
cout << "After clearing the list, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
nums.push_back(1);
|
||||
nums.push_back(3);
|
||||
nums.push_back(2);
|
||||
nums.push_back(5);
|
||||
nums.push_back(4);
|
||||
cout << "After adding elements, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums.insert(nums.begin() + 3, 6);
|
||||
cout << "Insert the number 6 at index 3, resulting in nums = ";
|
||||
printVector(nums);
|
||||
|
||||
/* 要素を削除 */
|
||||
nums.erase(nums.begin() + 3);
|
||||
cout << "Remove the element at index 3, resulting in 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つのリストを連結 */
|
||||
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 = ";
|
||||
printVector(nums);
|
||||
|
||||
/* リストをソート */
|
||||
sort(nums.begin(), nums.end());
|
||||
cout << "After sorting the list, nums = ";
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
/**
|
||||
* File: my_list.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* リストクラス */
|
||||
class MyList {
|
||||
private:
|
||||
int *arr; // 配列(リスト要素を格納)
|
||||
int arrCapacity = 10; // リストの容量
|
||||
int arrSize = 0; // リストの長さ(現在の要素数)
|
||||
int extendRatio = 2; // リスト拡張時の倍率
|
||||
|
||||
public:
|
||||
/* コンストラクタ */
|
||||
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");
|
||||
return arr[index];
|
||||
}
|
||||
|
||||
/* 要素を更新 */
|
||||
void set(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
arr[index] = num;
|
||||
}
|
||||
|
||||
/* 末尾に要素を追加 */
|
||||
void add(int num) {
|
||||
// 要素数が容量を超えた場合、拡張メカニズムをトリガー
|
||||
if (size() == capacity())
|
||||
extendCapacity();
|
||||
arr[size()] = num;
|
||||
// 要素数を更新
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
void insert(int index, int num) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
// 要素数が容量を超えた場合、拡張メカニズムをトリガー
|
||||
if (size() == capacity())
|
||||
extendCapacity();
|
||||
// `index`より後のすべての要素を1つ後ろに移動
|
||||
for (int j = size() - 1; j >= index; j--) {
|
||||
arr[j + 1] = arr[j];
|
||||
}
|
||||
arr[index] = num;
|
||||
// 要素数を更新
|
||||
arrSize++;
|
||||
}
|
||||
|
||||
/* 要素を削除 */
|
||||
int remove(int index) {
|
||||
if (index < 0 || index >= size())
|
||||
throw out_of_range("Index out of bounds");
|
||||
int num = arr[index];
|
||||
// `index`より後のすべての要素を1つ前に移動
|
||||
for (int j = index; j < size() - 1; j++) {
|
||||
arr[j] = arr[j + 1];
|
||||
}
|
||||
// 要素数を更新
|
||||
arrSize--;
|
||||
// 削除された要素を返却
|
||||
return num;
|
||||
}
|
||||
|
||||
/* リストを拡張 */
|
||||
void extendCapacity() {
|
||||
// 元の配列の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<int> toVector() {
|
||||
// 有効な長さ範囲内の要素のみを変換
|
||||
vector<int> vec(size());
|
||||
for (int i = 0; i < size(); i++) {
|
||||
vec[i] = arr[i];
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* リストを初期化 */
|
||||
MyList *nums = new MyList();
|
||||
/* 末尾に要素を追加 */
|
||||
nums->add(1);
|
||||
nums->add(3);
|
||||
nums->add(2);
|
||||
nums->add(5);
|
||||
nums->add(4);
|
||||
cout << "List nums = ";
|
||||
vector<int> vec = nums->toVector();
|
||||
printVector(vec);
|
||||
cout << "Capacity = " << nums->capacity() << ", length = " << nums->size() << endl;
|
||||
|
||||
/* 中間に要素を挿入 */
|
||||
nums->insert(3, 6);
|
||||
cout << "Insert the number 6 at index 3, resulting in nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 要素を削除 */
|
||||
nums->remove(3);
|
||||
cout << "Remove the element at index 3, resulting in nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 要素にアクセス */
|
||||
int num = nums->get(1);
|
||||
cout << "Access the element at index 1, obtained num = " << num << endl;
|
||||
|
||||
/* 要素を更新 */
|
||||
nums->set(1, 0);
|
||||
cout << "Update the element at index 1 to 0, resulting in nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
|
||||
/* 拡張メカニズムをテスト */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
// i = 5の時、リストの長さがリストの容量を超え、この時点で拡張メカニズムがトリガーされる
|
||||
nums->add(i);
|
||||
}
|
||||
cout << "After extending, list nums = ";
|
||||
vec = nums->toVector();
|
||||
printVector(vec);
|
||||
cout << "Capacity = " << nums->capacity() << ", length = " << nums->size() << endl;
|
||||
|
||||
// メモリを解放
|
||||
delete nums;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user