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:
@@ -24,7 +24,7 @@ public class complexity_exercises {
|
||||
return n + sumRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 線形時間のループ */
|
||||
/* 線形オーダーのループ */
|
||||
static int linearLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -33,7 +33,7 @@ public class complexity_exercises {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 二次時間のループ */
|
||||
/* 二次オーダーのループ */
|
||||
static int quadraticLoop(int n) {
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
@@ -44,7 +44,7 @@ public class complexity_exercises {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 対数時間のループ */
|
||||
/* 対数オーダーのループ */
|
||||
static int logarithmicLoop(int n) {
|
||||
while (n > 1) {
|
||||
n /= 2;
|
||||
|
||||
@@ -10,7 +10,7 @@ public class iteration {
|
||||
/* for ループ */
|
||||
static 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;
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public class iteration {
|
||||
static 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++; // 条件変数を更新する
|
||||
@@ -29,11 +29,11 @@ public class iteration {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while ループ(2回更新) */
|
||||
/* while ループ(2 回の更新) */
|
||||
static int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 条件変数を初期化する
|
||||
// 1, 4, 10, ... を順に加算する
|
||||
// ループで 1, 4, 10, ... の和を求める
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// 条件変数を更新する
|
||||
@@ -46,9 +46,9 @@ public class iteration {
|
||||
/* 二重 for ループ */
|
||||
static String nestedForLoop(int n) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
// 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.append("(" + i + ", " + j + "), ");
|
||||
}
|
||||
@@ -56,19 +56,19 @@ public class iteration {
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 実行用コード */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
System.out.println("\nfor ループの合計結果 res = " + res);
|
||||
System.out.println("\nfor ループで求めた和 res = " + res);
|
||||
|
||||
res = whileLoop(n);
|
||||
System.out.println("\nwhile ループの合計結果 res = " + res);
|
||||
System.out.println("\nwhile ループで求めた和 res = " + res);
|
||||
|
||||
res = whileLoopII(n);
|
||||
System.out.println("\nwhile ループ(2 回更新)の合計結果 res = " + res);
|
||||
System.out.println("\nwhile ループ(2 回の更新)で求めた和 res = " + res);
|
||||
|
||||
String resStr = nestedForLoop(n);
|
||||
System.out.println("\n二重 for ループの走査結果 " + resStr);
|
||||
|
||||
@@ -14,25 +14,25 @@ public class recursion {
|
||||
// 終了条件
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// 再帰:再帰呼び出し
|
||||
// 往路:再帰呼び出し
|
||||
int res = recur(n - 1);
|
||||
// 帰りがけ:結果を返す
|
||||
// 復路:結果を返す
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* 反復で再帰を模擬する */
|
||||
/* 反復を使って再帰を再現する */
|
||||
static int forLoopRecur(int n) {
|
||||
// 明示的なスタックを使ってシステムコールスタックを模擬する
|
||||
// 明示的なスタックを使って、システムの呼び出しスタックを再現する
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
int res = 0;
|
||||
// 再帰:再帰呼び出し
|
||||
// 往路:再帰呼び出し
|
||||
for (int i = n; i > 0; i--) {
|
||||
// 「スタックへのプッシュ」で「再帰」を模擬する
|
||||
// 「プッシュ操作」で再帰の「往路」を再現する
|
||||
stack.push(i);
|
||||
}
|
||||
// 帰りがけ:結果を返す
|
||||
// 復路:結果を返す
|
||||
while (!stack.isEmpty()) {
|
||||
// 「スタックから取り出す操作」で「帰り」をシミュレート
|
||||
// 「ポップ操作」で再帰の「復路」を再現する
|
||||
res += stack.pop();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
@@ -53,25 +53,25 @@ public class recursion {
|
||||
// 終了条件 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 */
|
||||
/* 実行用コード */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
System.out.println("\n再帰関数の合計結果 res = " + res);
|
||||
System.out.println("\n再帰関数で求めた和 res = " + res);
|
||||
|
||||
res = forLoopRecur(n);
|
||||
System.out.println("\n反復による再帰シミュレーションの合計結果 res = " + res);
|
||||
System.out.println("\n反復で再帰を再現して求めた和 res = " + res);
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
System.out.println("\n末尾再帰関数の合計結果 res = " + res);
|
||||
System.out.println("\n末尾再帰関数で求めた和 res = " + res);
|
||||
|
||||
res = fib(n);
|
||||
System.out.println("\nフィボナッチ数列の第 " + n + " 項は " + res);
|
||||
|
||||
@@ -12,44 +12,44 @@ import java.util.*;
|
||||
public class space_complexity {
|
||||
/* 関数 */
|
||||
static int function() {
|
||||
// 何らかの処理を行う
|
||||
// 何らかの処理を実行する
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 定数階 */
|
||||
/* 定数オーダー */
|
||||
static void constant(int n) {
|
||||
// 定数、変数、オブジェクトは O(1) の空間を占める
|
||||
// 定数、変数、オブジェクトは O(1) の領域を使用する
|
||||
final int a = 0;
|
||||
int b = 0;
|
||||
int[] nums = new int[10000];
|
||||
ListNode node = new ListNode(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++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
|
||||
/* 線形階 */
|
||||
/* 線形オーダー */
|
||||
static void linear(int n) {
|
||||
// 長さ n の配列は O(n) の空間を使用
|
||||
// 長さ n の配列は O(n) の領域を使用する
|
||||
int[] nums = new int[n];
|
||||
// 長さ n のリストは O(n) の空間を使用
|
||||
// 長さ n のリストは O(n) の領域を使用する
|
||||
List<ListNode> nodes = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.add(new ListNode(i));
|
||||
}
|
||||
// 長さ n のハッシュテーブルは O(n) の空間を使用
|
||||
// 長さ n のハッシュテーブルは O(n) の領域を使用する
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
map.put(i, String.valueOf(i));
|
||||
}
|
||||
}
|
||||
|
||||
/* 線形時間(再帰実装) */
|
||||
/* 線形オーダー(再帰による実装) */
|
||||
static void linearRecur(int n) {
|
||||
System.out.println("再帰 n = " + n);
|
||||
if (n == 1)
|
||||
@@ -57,11 +57,11 @@ public class space_complexity {
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 二乗階 */
|
||||
/* 二次オーダー */
|
||||
static void quadratic(int n) {
|
||||
// 行列は O(n^2) の空間を使用する
|
||||
// 行列は O(n^2) の領域を使用する
|
||||
int[][] numMatrix = new int[n][n];
|
||||
// 二次元リストは O(n^2) の空間を使用
|
||||
// 二次元リストは O(n^2) の領域を使用する
|
||||
List<List<Integer>> numList = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
@@ -72,7 +72,7 @@ public class space_complexity {
|
||||
}
|
||||
}
|
||||
|
||||
/* 二次時間(再帰実装) */
|
||||
/* 二次オーダー(再帰による実装) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
@@ -82,7 +82,7 @@ public class space_complexity {
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数時間(完全二分木の構築) */
|
||||
/* 指数オーダー(パーフェクト二分木の構築) */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0)
|
||||
return null;
|
||||
@@ -92,15 +92,15 @@ public class space_complexity {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 実行用コード */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
// 定数階
|
||||
// 定数オーダー
|
||||
constant(n);
|
||||
// 線形階
|
||||
// 線形オーダー
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// 二乗階
|
||||
// 二次オーダー
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// 指数オーダー
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class time_complexity {
|
||||
/* 定数階 */
|
||||
/* 定数オーダー */
|
||||
static int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
@@ -16,7 +16,7 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 線形階 */
|
||||
/* 線形オーダー */
|
||||
static int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
@@ -24,20 +24,20 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 線形時間(配列を走査) */
|
||||
/* 線形オーダー(配列の走査) */
|
||||
static int arrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// ループ回数は配列長に比例する
|
||||
// ループの回数は配列の長さに比例する
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 二乗階 */
|
||||
/* 二次オーダー */
|
||||
static int quadratic(int n) {
|
||||
int count = 0;
|
||||
// ループ回数はデータサイズ n の二乗に比例する
|
||||
// ループの回数はデータの大きさ n の二乗に比例する
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
@@ -46,29 +46,29 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 二次時間(バブルソート) */
|
||||
/* 二次オーダー(バブルソート) */
|
||||
static int bubbleSort(int[] nums) {
|
||||
int count = 0; // カウンタ
|
||||
// 外側のループ:未ソート区間は [0, i]
|
||||
for (int i = nums.length - 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;
|
||||
}
|
||||
|
||||
/* 指数時間(ループ実装) */
|
||||
/* 指数オーダー(ループによる実装) */
|
||||
static 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++;
|
||||
@@ -79,14 +79,14 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 指数時間(再帰実装) */
|
||||
/* 指数オーダー(再帰による実装) */
|
||||
static int expRecur(int n) {
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* 対数時間(ループ実装) */
|
||||
/* 対数オーダー(ループによる実装) */
|
||||
static int logarithmic(int n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
@@ -96,14 +96,14 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 対数時間(再帰実装) */
|
||||
/* 対数オーダー(再帰による実装) */
|
||||
static int logRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 線形対数時間 */
|
||||
/* 線形対数オーダー */
|
||||
static int linearLogRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
@@ -114,54 +114,54 @@ public class time_complexity {
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 階乗時間(再帰実装) */
|
||||
/* 階乗オーダー(再帰による実装) */
|
||||
static 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 */
|
||||
/* 実行用コード */
|
||||
public static void main(String[] args) {
|
||||
// n を変えて実行し、各計算量で操作回数がどう変化するかを確認できる
|
||||
// n を変更して実行し、各計算量で操作回数がどのように変化するか確かめられる
|
||||
int n = 8;
|
||||
System.out.println("入力データサイズ n = " + n);
|
||||
System.out.println("入力データの大きさ n = " + n);
|
||||
|
||||
int count = constant(n);
|
||||
System.out.println("定数時間の操作回数 = " + count);
|
||||
System.out.println("定数オーダーの操作回数 = " + count);
|
||||
|
||||
count = linear(n);
|
||||
System.out.println("線形時間の操作回数 = " + count);
|
||||
System.out.println("線形オーダーの操作回数 = " + count);
|
||||
count = arrayTraversal(new int[n]);
|
||||
System.out.println("線形時間(配列走査)の操作回数 = " + count);
|
||||
System.out.println("線形オーダー(配列の走査)の操作回数 = " + count);
|
||||
|
||||
count = quadratic(n);
|
||||
System.out.println("2 次時間の操作回数 = " + count);
|
||||
System.out.println("二次オーダーの操作回数 = " + count);
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
System.out.println("2 次時間(バブルソート)の操作回数 = " + count);
|
||||
System.out.println("二次オーダー(バブルソート)の操作回数 = " + count);
|
||||
|
||||
count = exponential(n);
|
||||
System.out.println("指数時間(ループ実装)の操作回数 = " + count);
|
||||
System.out.println("指数オーダー(ループによる実装)の操作回数 = " + count);
|
||||
count = expRecur(n);
|
||||
System.out.println("指数時間(再帰実装)の操作回数 = " + count);
|
||||
System.out.println("指数オーダー(再帰による実装)の操作回数 = " + count);
|
||||
|
||||
count = logarithmic(n);
|
||||
System.out.println("対数時間(ループ実装)の操作回数 = " + count);
|
||||
System.out.println("対数オーダー(ループによる実装)の操作回数 = " + count);
|
||||
count = logRecur(n);
|
||||
System.out.println("対数時間(再帰実装)の操作回数 = " + count);
|
||||
System.out.println("対数オーダー(再帰による実装)の操作回数 = " + count);
|
||||
|
||||
count = linearLogRecur(n);
|
||||
System.out.println("線形対数時間(再帰実装)の操作回数 = " + count);
|
||||
System.out.println("線形対数オーダー(再帰による実装)の操作回数 = " + count);
|
||||
|
||||
count = factorialRecur(n);
|
||||
System.out.println("階乗時間(再帰実装)の操作回数 = " + count);
|
||||
System.out.println("階乗オーダー(再帰による実装)の操作回数 = " + count);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,14 +9,14 @@ package chapter_computational_complexity;
|
||||
import java.util.*;
|
||||
|
||||
public class worst_best_time_complexity {
|
||||
/* 要素が { 1, 2, ..., n } で、順序がシャッフルされた配列を生成 */
|
||||
/* 要素が { 1, 2, ..., n } で、順序をシャッフルした配列を生成する */
|
||||
static int[] randomNumbers(int n) {
|
||||
Integer[] nums = new Integer[n];
|
||||
// 配列 nums = { 1, 2, 3, ..., n } を生成
|
||||
// 配列 nums = { 1, 2, 3, ..., n } を生成する
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// 配列要素をランダムにシャッフル
|
||||
// 配列の要素をランダムに並べ替える
|
||||
Collections.shuffle(Arrays.asList(nums));
|
||||
// Integer[] -> int[]
|
||||
int[] res = new int[n];
|
||||
@@ -26,25 +26,25 @@ public class worst_best_time_complexity {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 配列 nums 内で数値 1 のインデックスを探す */
|
||||
/* 配列 nums 内で数値 1 のインデックスを探索する */
|
||||
static int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// 要素 1 が配列の先頭にあるとき、最良時間計算量 O(1) となる
|
||||
// 要素 1 が配列の末尾にあるとき、最悪時間計算量 O(n) となる
|
||||
// 要素 1 が配列の先頭にある場合、最良時間計算量 O(1) となる
|
||||
// 要素 1 が配列の末尾にある場合、最悪時間計算量 O(n) となる
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 実行用コード */
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
System.out.println("\n配列 [ 1, 2, ..., n ] をシャッフルした後 = " + Arrays.toString(nums));
|
||||
System.out.println("数字 1 のインデックスは " + index);
|
||||
System.out.println("\n配列 [ 1, 2, ..., n ] をシャッフルした結果 = " + Arrays.toString(nums));
|
||||
System.out.println("数値 1 のインデックスは " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user