mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
This commit is contained in:
@@ -23,7 +23,7 @@ int sumRecur(int n) {
|
||||
return n + sumRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 線形時間のループ */
|
||||
/* 線形オーダーのループ */
|
||||
int linearLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
@@ -32,7 +32,7 @@ int linearLoop(int n) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 二次時間のループ */
|
||||
/* 二次オーダーのループ */
|
||||
int quadraticLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
@@ -43,7 +43,7 @@ int quadraticLoop(int n) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 対数時間のループ */
|
||||
/* 対数オーダーのループ */
|
||||
int logarithmicLoop(int n) {
|
||||
while (n > 1) {
|
||||
n /= 2;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
/* for ループ */
|
||||
int forLoop(int n) {
|
||||
int res = 0;
|
||||
// 1, 2, ..., n-1, n を順に加算する
|
||||
// ループで 1, 2, ..., n-1, n の和を求める
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
res += i;
|
||||
}
|
||||
@@ -20,7 +20,7 @@ int forLoop(int n) {
|
||||
int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 条件変数を初期化する
|
||||
// 1, 2, ..., n-1, n を順に加算する
|
||||
// ループで 1, 2, ..., n-1, n の和を求める
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // 条件変数を更新する
|
||||
@@ -28,11 +28,11 @@ int whileLoop(int n) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while ループ(2回更新) */
|
||||
/* while ループ(2 回の更新) */
|
||||
int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 条件変数を初期化する
|
||||
// 1, 4, 10, ... を順に加算する
|
||||
// ループで 1, 4, 10, ... の和を求める
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// 条件変数を更新する
|
||||
@@ -45,9 +45,9 @@ int whileLoopII(int n) {
|
||||
/* 二重 for ループ */
|
||||
string nestedForLoop(int n) {
|
||||
ostringstream res;
|
||||
// i = 1, 2, ..., n-1, n とループする
|
||||
// i = 1, 2, ..., n-1, n のループ
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
// j = 1, 2, ..., n-1, n とループする
|
||||
// j = 1, 2, ..., n-1, n のループ
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
res << "(" << i << ", " << j << "), ";
|
||||
}
|
||||
@@ -55,19 +55,19 @@ string nestedForLoop(int n) {
|
||||
return res.str();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 実行用コード */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
cout << "\nfor ループの合計結果 res = " << res << endl;
|
||||
cout << "\nfor ループで求めた和 res = " << res << endl;
|
||||
|
||||
res = whileLoop(n);
|
||||
cout << "\nwhile ループの合計結果 res = " << res << endl;
|
||||
cout << "\nwhile ループで求めた和 res = " << res << endl;
|
||||
|
||||
res = whileLoopII(n);
|
||||
cout << "\nwhile ループ(2 回更新)の合計結果 res = " << res << endl;
|
||||
cout << "\nwhile ループ(2 回の更新)で求めた和 res = " << res << endl;
|
||||
|
||||
string resStr = nestedForLoop(n);
|
||||
cout << "\n二重 for ループの走査結果 " << resStr << endl;
|
||||
|
||||
@@ -11,25 +11,25 @@ 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();
|
||||
}
|
||||
@@ -51,25 +51,25 @@ 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) を再帰的に呼び出す
|
||||
// 再帰呼び出し f(n) = f(n-1) + f(n-2)
|
||||
int res = fib(n - 1) + fib(n - 2);
|
||||
// 結果 f(n) を返す
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 実行用コード */
|
||||
int main() {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
cout << "\n再帰関数の合計結果 res = " << res << endl;
|
||||
cout << "\n再帰関数で求めた和 res = " << res << endl;
|
||||
|
||||
res = forLoopRecur(n);
|
||||
cout << "\n反復で再帰をシミュレートした合計結果 res = " << res << endl;
|
||||
cout << "\n反復で再帰を再現して求めた和 res = " << res << endl;
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
cout << "\n末尾再帰関数の合計結果 res = " << res << endl;
|
||||
cout << "\n末尾再帰関数で求めた和 res = " << res << endl;
|
||||
|
||||
res = fib(n);
|
||||
cout << "\nフィボナッチ数列の第 " << n << " 項は " << res << endl;
|
||||
|
||||
@@ -8,44 +8,44 @@
|
||||
|
||||
/* 関数 */
|
||||
int func() {
|
||||
// 何らかの処理を行う
|
||||
// 何らかの処理を実行する
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 定数階 */
|
||||
/* 定数オーダー */
|
||||
void constant(int n) {
|
||||
// 定数、変数、オブジェクトは O(1) の空間を占める
|
||||
// 定数、変数、オブジェクトは O(1) の領域を使用する
|
||||
const int a = 0;
|
||||
int b = 0;
|
||||
vector<int> nums(10000);
|
||||
ListNode node(0);
|
||||
// ループ内の変数は O(1) の空間を占める
|
||||
// ループ内の変数は O(1) の領域を使用する
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// ループ内の関数は O(1) の空間を占める
|
||||
// ループ内の関数は O(1) の領域を使用する
|
||||
for (int i = 0; i < n; i++) {
|
||||
func();
|
||||
}
|
||||
}
|
||||
|
||||
/* 線形階 */
|
||||
/* 線形オーダー */
|
||||
void linear(int n) {
|
||||
// 長さ n の配列は O(n) の空間を使用
|
||||
// 長さ n の配列は O(n) の領域を使用する
|
||||
vector<int> nums(n);
|
||||
// 長さ n のリストは O(n) の空間を使用
|
||||
// 長さ n のリストは O(n) の領域を使用する
|
||||
vector<ListNode> nodes;
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.push_back(ListNode(i));
|
||||
}
|
||||
// 長さ n のハッシュテーブルは O(n) の空間を使用
|
||||
// 長さ 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)
|
||||
@@ -53,9 +53,9 @@ void linearRecur(int n) {
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 二乗階 */
|
||||
/* 二次オーダー */
|
||||
void quadratic(int n) {
|
||||
// 二次元リストは O(n^2) の空間を使用
|
||||
// 二次元リストは O(n^2) の領域を使用する
|
||||
vector<vector<int>> numMatrix;
|
||||
for (int i = 0; i < n; i++) {
|
||||
vector<int> tmp;
|
||||
@@ -66,7 +66,7 @@ void quadratic(int n) {
|
||||
}
|
||||
}
|
||||
|
||||
/* 二次時間(再帰実装) */
|
||||
/* 二次オーダー(再帰による実装) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
@@ -75,7 +75,7 @@ int quadraticRecur(int n) {
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数時間(完全二分木の構築) */
|
||||
/* 指数オーダー(パーフェクト二分木の構築) */
|
||||
TreeNode *buildTree(int n) {
|
||||
if (n == 0)
|
||||
return nullptr;
|
||||
@@ -85,15 +85,15 @@ TreeNode *buildTree(int n) {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 実行用コード */
|
||||
int main() {
|
||||
int n = 5;
|
||||
// 定数階
|
||||
// 定数オーダー
|
||||
constant(n);
|
||||
// 線形階
|
||||
// 線形オーダー
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// 二乗階
|
||||
// 二次オーダー
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// 指数オーダー
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 定数階 */
|
||||
/* 定数オーダー */
|
||||
int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
@@ -15,7 +15,7 @@ int constant(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 線形階 */
|
||||
/* 線形オーダー */
|
||||
int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
@@ -23,20 +23,20 @@ int linear(int n) {
|
||||
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 の二乗に比例する
|
||||
// ループの回数はデータの大きさ n の二乗に比例する
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
@@ -45,29 +45,29 @@ int quadratic(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 二次時間(バブルソート) */
|
||||
/* 二次オーダー(バブルソート) */
|
||||
int bubbleSort(vector<int> &nums) {
|
||||
int count = 0; // カウンタ
|
||||
// 外側のループ:未ソート区間は [0, i]
|
||||
for (int i = nums.size() - 1; i > 0; i--) {
|
||||
// 内側のループ:未ソート区間 [0, i] の最大要素をその区間の最右端へ交換
|
||||
// 内側のループ:未ソート区間 [0, i] の最大要素を区間の右端に移す
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 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 回の単位操作が含まれる
|
||||
count += 3; // 要素の交換は 3 回の単位操作からなる
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 指数時間(ループ実装) */
|
||||
/* 指数オーダー(ループによる実装) */
|
||||
int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// 細胞は各ラウンドで 2 つに分裂し、数列 1, 2, 4, 8, ..., 2^(n-1) を形成する
|
||||
// 各回で細胞が 2 つに分裂し、数列 1, 2, 4, 8, ..., 2^(n-1) を形成する
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
@@ -78,14 +78,14 @@ int exponential(int n) {
|
||||
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) {
|
||||
@@ -95,14 +95,14 @@ int logarithmic(int n) {
|
||||
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;
|
||||
@@ -113,23 +113,23 @@ int linearLogRecur(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 階乗時間(再帰実装) */
|
||||
/* 階乗オーダー(再帰による実装) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// 1個から n 個に分裂
|
||||
// 1 個から n 個に分裂する
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 実行用コード */
|
||||
int main() {
|
||||
// n を変えて実行し、各計算量で操作回数がどう変化するかを確認できる
|
||||
// n を変更して実行し、各計算量で操作回数がどのように変化するか確かめられる
|
||||
int n = 8;
|
||||
cout << "入力データサイズ n = " << n << endl;
|
||||
cout << "入力データの大きさ n = " << n << endl;
|
||||
|
||||
int count = constant(n);
|
||||
cout << "定数オーダーの操作回数 = " << count << endl;
|
||||
@@ -138,31 +138,31 @@ int main() {
|
||||
cout << "線形オーダーの操作回数 = " << count << endl;
|
||||
vector<int> arr(n);
|
||||
count = arrayTraversal(arr);
|
||||
cout << "線形オーダー(配列走査)の操作回数 = " << count << endl;
|
||||
cout << "線形オーダー(配列の走査)の操作回数 = " << count << endl;
|
||||
|
||||
count = quadratic(n);
|
||||
cout << "二乗オーダーの操作回数 = " << count << endl;
|
||||
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;
|
||||
cout << "二次オーダー(バブルソート)の操作回数 = " << count << endl;
|
||||
|
||||
count = exponential(n);
|
||||
cout << "指数オーダー(ループ実装)の操作回数 = " << count << endl;
|
||||
cout << "指数オーダー(ループによる実装)の操作回数 = " << count << endl;
|
||||
count = expRecur(n);
|
||||
cout << "指数オーダー(再帰実装)の操作回数 = " << count << endl;
|
||||
cout << "指数オーダー(再帰による実装)の操作回数 = " << count << endl;
|
||||
|
||||
count = logarithmic(n);
|
||||
cout << "対数オーダー(ループ実装)の操作回数 = " << count << endl;
|
||||
cout << "対数オーダー(ループによる実装)の操作回数 = " << count << endl;
|
||||
count = logRecur(n);
|
||||
cout << "対数オーダー(再帰実装)の操作回数 = " << count << endl;
|
||||
cout << "対数オーダー(再帰による実装)の操作回数 = " << count << endl;
|
||||
|
||||
count = linearLogRecur(n);
|
||||
cout << "線形対数オーダー(再帰実装)の操作回数 = " << count << endl;
|
||||
cout << "線形対数オーダー(再帰による実装)の操作回数 = " << count << endl;
|
||||
|
||||
count = factorialRecur(n);
|
||||
cout << "階乗オーダー(再帰実装)の操作回数 = " << count << endl;
|
||||
cout << "階乗オーダー(再帰による実装)の操作回数 = " << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,40 +6,40 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 要素が { 1, 2, ..., n } で、順序がシャッフルされた配列を生成 */
|
||||
/* 要素が { 1, 2, ..., n } で、順序をシャッフルした配列を生成する */
|
||||
vector<int> randomNumbers(int n) {
|
||||
vector<int> nums(n);
|
||||
// 配列 nums = { 1, 2, 3, ..., 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 のインデックスを探す */
|
||||
/* 配列 nums 内で数値 1 のインデックスを探索する */
|
||||
int findOne(vector<int> &nums) {
|
||||
for (int i = 0; i < nums.size(); i++) {
|
||||
// 要素 1 が配列の先頭にあるとき、最良時間計算量 O(1) となる
|
||||
// 要素 1 が配列の末尾にあるとき、最悪時間計算量 O(n) となる
|
||||
// 要素 1 が配列の先頭にある場合、最良時間計算量 O(1) となる
|
||||
// 要素 1 が配列の末尾にある場合、最悪時間計算量 O(n) となる
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 実行用コード */
|
||||
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 ] をシャッフルした後 = ";
|
||||
cout << "\n配列 [ 1, 2, ..., n ] をシャッフルした結果 = ";
|
||||
printVector(nums);
|
||||
cout << "数字 1 のインデックスは " << index << endl;
|
||||
cout << "数値 1 のインデックスは " << index << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user