mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08: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
76
ja/codes/cpp/chapter_computational_complexity/iteration.cpp
Normal file
76
ja/codes/cpp/chapter_computational_complexity/iteration.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* File: iteration.cpp
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* for ループ */
|
||||
int forLoop(int n) {
|
||||
int res = 0;
|
||||
// 1, 2, ..., n-1, n の合計をループ計算
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while ループ */
|
||||
int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 条件変数を初期化
|
||||
// 1, 2, ..., n-1, n の合計をループ計算
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // 条件変数を更新
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while ループ(2つの更新) */
|
||||
int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 条件変数を初期化
|
||||
// 1, 4, 10, ... の合計をループ計算
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// 条件変数を更新
|
||||
i++;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 2重 for ループ */
|
||||
string nestedForLoop(int n) {
|
||||
ostringstream res;
|
||||
// ループ i = 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
// ループ j = 1, 2, ..., n-1, n
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
res << "(" << i << ", " << j << "), ";
|
||||
}
|
||||
}
|
||||
return res.str();
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
cout << "\nfor ループの合計結果 res = " << res << endl;
|
||||
|
||||
res = whileLoop(n);
|
||||
cout << "\nwhile ループの合計結果 res = " << res << endl;
|
||||
|
||||
res = whileLoopII(n);
|
||||
cout << "\nwhile ループ(2つの更新)の合計結果 res = " << res << endl;
|
||||
|
||||
string resStr = nestedForLoop(n);
|
||||
cout << "\n2重 for ループ走査の結果 = " << resStr << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
78
ja/codes/cpp/chapter_computational_complexity/recursion.cpp
Normal file
78
ja/codes/cpp/chapter_computational_complexity/recursion.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
/**
|
||||
* File: recursion.cpp
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 再帰 */
|
||||
int recur(int n) {
|
||||
// 終了条件
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// 再帰:再帰呼び出し
|
||||
int res = recur(n - 1);
|
||||
// 戻り値:結果を返す
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* 反復で再帰をシミュレート */
|
||||
int forLoopRecur(int n) {
|
||||
// 明示的なスタックを使用してシステムコールスタックをシミュレート
|
||||
stack<int> stack;
|
||||
int res = 0;
|
||||
// 再帰:再帰呼び出し
|
||||
for (int i = n; i > 0; i--) {
|
||||
// 「スタックへのプッシュ」で「再帰」をシミュレート
|
||||
stack.push(i);
|
||||
}
|
||||
// 戻り値:結果を返す
|
||||
while (!stack.empty()) {
|
||||
// 「スタックからのポップ」で「戻り値」をシミュレート
|
||||
res += stack.top();
|
||||
stack.pop();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 末尾再帰 */
|
||||
int tailRecur(int n, int res) {
|
||||
// 終了条件
|
||||
if (n == 0)
|
||||
return res;
|
||||
// 末尾再帰呼び出し
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* フィボナッチ数列:再帰 */
|
||||
int fib(int n) {
|
||||
// 終了条件 f(1) = 0, f(2) = 1
|
||||
if (n == 1 || n == 2)
|
||||
return n - 1;
|
||||
// 再帰呼び出し f(n) = f(n-1) + f(n-2)
|
||||
int res = fib(n - 1) + fib(n - 2);
|
||||
// 結果 f(n) を返す
|
||||
return res;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
cout << "\n再帰関数の合計結果 res = " << res << endl;
|
||||
|
||||
res = forLoopRecur(n);
|
||||
cout << "\n反復を使用して再帰をシミュレートした合計結果 res = " << res << endl;
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
cout << "\n末尾再帰関数の合計結果 res = " << res << endl;
|
||||
|
||||
res = fib(n);
|
||||
cout << "フィボナッチ数列の第 " << n << " 番目の数は " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,107 @@
|
||||
/**
|
||||
* File: space_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 関数 */
|
||||
int func() {
|
||||
// 何らかの操作を実行
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 定数計算量 */
|
||||
void constant(int n) {
|
||||
// 定数、変数、オブジェクトは O(1) 空間を占める
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
vector<int> nums(10000);
|
||||
ListNode node(0);
|
||||
// ループ内の変数は O(1) 空間を占める
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// ループ内の関数は O(1) 空間を占める
|
||||
for (int i = 0; i < n; i++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
/* 線形計算量 */
|
||||
void linear(int n) {
|
||||
// 長さ n の配列は O(n) 空間を占める
|
||||
vector<int> nums(n);
|
||||
// 長さ n のリストは O(n) 空間を占める
|
||||
vector<ListNode> nodes;
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.push_back(ListNode(i));
|
||||
}
|
||||
// 長さ n のハッシュテーブルは O(n) 空間を占める
|
||||
unordered_map<int, string> map;
|
||||
for (int i = 0; i < n; i++) {
|
||||
map[i] = to_string(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* 線形計算量(再帰実装) */
|
||||
void linearRecur(int n) {
|
||||
cout << "再帰 n = " << n << endl;
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 二次計算量 */
|
||||
void quadratic(int n) {
|
||||
// 二次元リストは O(n^2) 空間を占める
|
||||
vector<vector<int>> numMatrix;
|
||||
for (int i = 0; i < n; i++) {
|
||||
vector<int> tmp;
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.push_back(0);
|
||||
}
|
||||
numMatrix.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 二次計算量(再帰実装) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "再帰 n = " << n << ", nums の長さ = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数計算量(完全二分木の構築) */
|
||||
TreeNode *buildTree(int n) {
|
||||
if (n == 0)
|
||||
return nullptr;
|
||||
TreeNode *root = new TreeNode(0);
|
||||
root->left = buildTree(n - 1);
|
||||
root->right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
int n = 5;
|
||||
// 定数計算量
|
||||
constant(n);
|
||||
// 線形計算量
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// 二次計算量
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// 指数計算量
|
||||
TreeNode *root = buildTree(n);
|
||||
printTree(root);
|
||||
|
||||
// メモリを解放
|
||||
freeMemoryTree(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,168 @@
|
||||
/**
|
||||
* File: time_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 定数計算量 */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 線形計算量 */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 線形計算量(配列の走査) */
|
||||
int arrayTraversal(vector<int> &nums) {
|
||||
int count = 0;
|
||||
// ループ回数は配列の長さに比例
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 二次計算量 */
|
||||
int quadratic(int n) {
|
||||
int count = 0;
|
||||
// ループ回数はデータサイズ n の二乗に比例
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 二次計算量(バブルソート) */
|
||||
int bubbleSort(vector<int> &nums) {
|
||||
int count = 0; // カウンター
|
||||
// 外側ループ:未ソート範囲は [0, i]
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
// 内側ループ:未ソート範囲 [0, i] の最大要素を範囲の右端にスワップ
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// nums[j] と nums[j + 1] をスワップ
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 要素のスワップには3つの個別操作が含まれる
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 指数計算量(ループ実装) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// セルは毎ラウンド2つに分裂し、数列 1, 2, 4, 8, ..., 2^(n-1) を形成
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
}
|
||||
base *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 指数計算量(再帰実装) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* 対数計算量(ループ実装) */
|
||||
int logarithmic(int n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 対数計算量(再帰実装) */
|
||||
int logRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 線形対数計算量 */
|
||||
int linearLogRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 階乗計算量(再帰実装) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// 1から n に分裂
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
// n を変更して、さまざまな計算量での操作回数の変化傾向を体験可能
|
||||
int n = 8;
|
||||
cout << "入力データサイズ n = " << n << endl;
|
||||
|
||||
int count = constant(n);
|
||||
cout << "定数計算量の操作回数 = " << count << endl;
|
||||
|
||||
count = linear(n);
|
||||
cout << "線形計算量の操作回数 = " << count << endl;
|
||||
vector<int> arr(n);
|
||||
count = arrayTraversal(arr);
|
||||
cout << "線形計算量の操作回数(配列走査) = " << count << endl;
|
||||
|
||||
count = quadratic(n);
|
||||
cout << "二次計算量の操作回数 = " << count << endl;
|
||||
vector<int> nums(n);
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
cout << "二次計算量の操作回数(バブルソート) = " << count << endl;
|
||||
|
||||
count = exponential(n);
|
||||
cout << "指数計算量の操作回数(ループ実装) = " << count << endl;
|
||||
count = expRecur(n);
|
||||
cout << "指数計算量の操作回数(再帰実装) = " << count << endl;
|
||||
|
||||
count = logarithmic(n);
|
||||
cout << "対数計算量の操作回数(ループ実装) = " << count << endl;
|
||||
count = logRecur(n);
|
||||
cout << "対数計算量の操作回数(再帰実装) = " << count << endl;
|
||||
|
||||
count = linearLogRecur(n);
|
||||
cout << "線形対数計算量の操作回数(再帰実装) = " << count << endl;
|
||||
|
||||
count = factorialRecur(n);
|
||||
cout << "階乗計算量の操作回数(再帰実装) = " << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* File: worst_best_time_complexity.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 要素 {1, 2, ..., n} をランダムにシャッフルした配列を生成 */
|
||||
vector<int> randomNumbers(int n) {
|
||||
vector<int> nums(n);
|
||||
// 配列 nums = { 1, 2, 3, ..., n } を生成
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// システム時刻を使用してランダムシードを生成
|
||||
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
|
||||
// 配列要素をランダムにシャッフル
|
||||
shuffle(nums.begin(), nums.end(), default_random_engine(seed));
|
||||
return nums;
|
||||
}
|
||||
|
||||
/* 配列 nums で数値1のインデックスを見つける */
|
||||
int findOne(vector<int> &nums) {
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// 要素1が配列の先頭にある場合、最良時間計算量 O(1) を達成
|
||||
// 要素1が配列の末尾にある場合、最悪時間計算量 O(n) を達成
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
int n = 100;
|
||||
vector<int> nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
cout << "\n配列 [ 1, 2, ..., n ] をシャッフル後 = ";
|
||||
printVector(nums);
|
||||
cout << "数値1のインデックスは " << index << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user