mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-15 14:48:05 +08: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:
@@ -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;
|
||||
}
|
||||
@@ -20,35 +20,35 @@ public class iteration {
|
||||
/* while ループ */
|
||||
static int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 条件変数を初期化
|
||||
// 1, 2, ..., n-1, n の合計をループ計算
|
||||
int i = 1; // 条件変数を初期化する
|
||||
// 1, 2, ..., n-1, n を順に加算する
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // 条件変数を更新
|
||||
i++; // 条件変数を更新する
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while ループ(2つの更新) */
|
||||
/* while ループ(2回更新) */
|
||||
static int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // 条件変数を初期化
|
||||
// 1, 4, 10, ... の合計をループ計算
|
||||
int i = 1; // 条件変数を初期化する
|
||||
// 1, 4, 10, ... を順に加算する
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// 条件変数を更新
|
||||
// 条件変数を更新する
|
||||
i++;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 2重 for ループ */
|
||||
/* 二重 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,7 +56,7 @@ public class iteration {
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int res;
|
||||
@@ -68,9 +68,9 @@ public class iteration {
|
||||
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("\n2重 for ループ走査の結果 = " + resStr);
|
||||
System.out.println("\n二重 for ループの走査結果 " + resStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,23 +16,23 @@ public class recursion {
|
||||
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,13 +53,13 @@ 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;
|
||||
@@ -68,12 +68,12 @@ public class recursion {
|
||||
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);
|
||||
|
||||
res = fib(n);
|
||||
System.out.println("\nフィボナッチ数列の第 " + n + " 番目の数は " + res);
|
||||
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,17 +72,17 @@ public class space_complexity {
|
||||
}
|
||||
}
|
||||
|
||||
/* 二次計算量(再帰実装) */
|
||||
/* 二次時間(再帰実装) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
// 配列 nums の長さ = n, n-1, ..., 2, 1
|
||||
// 配列 nums の長さは n, n-1, ..., 2, 1
|
||||
int[] nums = new int[n];
|
||||
System.out.println("再帰 n = " + n + " の nums の長さ = " + nums.length);
|
||||
System.out.println("再帰 n = " + n + " における nums の長さ = " + nums.length);
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数計算量(完全二分木の構築) */
|
||||
/* 指数時間(完全二分木の構築) */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0)
|
||||
return null;
|
||||
@@ -92,19 +92,19 @@ 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);
|
||||
// 指数計算量
|
||||
// 指数オーダー
|
||||
TreeNode root = buildTree(n);
|
||||
PrintUtil.printTree(root);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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]
|
||||
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);
|
||||
|
||||
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("二次計算量の操作回数 = " + count);
|
||||
System.out.println("2 次時間の操作回数 = " + 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("二次計算量の操作回数(バブルソート) = " + count);
|
||||
System.out.println("2 次時間(バブルソート)の操作回数 = " + 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,7 +9,7 @@ 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 } を生成
|
||||
@@ -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