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
@ -0,0 +1,76 @@
|
||||
/**
|
||||
* File: iteration.java
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class iteration {
|
||||
/* for ループ */
|
||||
static int forLoop(int n) {
|
||||
int res = 0;
|
||||
// 1, 2, ..., n-1, n の合計をループ計算
|
||||
for (int i = 1; i <= n; i++) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while ループ */
|
||||
static 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つの更新) */
|
||||
static 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 ループ */
|
||||
static String nestedForLoop(int n) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
// ループ 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.append("(" + i + ", " + j + "), ");
|
||||
}
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
System.out.println("\nfor ループの合計結果 res = " + res);
|
||||
|
||||
res = whileLoop(n);
|
||||
System.out.println("\nwhile ループの合計結果 res = " + res);
|
||||
|
||||
res = whileLoopII(n);
|
||||
System.out.println("\nwhile ループ(2つの更新)の合計結果 res = " + res);
|
||||
|
||||
String resStr = nestedForLoop(n);
|
||||
System.out.println("\n2重 for ループ走査の結果 = " + resStr);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* File: recursion.java
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class recursion {
|
||||
/* 再帰 */
|
||||
static int recur(int n) {
|
||||
// 終了条件
|
||||
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
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 末尾再帰 */
|
||||
static int tailRecur(int n, int res) {
|
||||
// 終了条件
|
||||
if (n == 0)
|
||||
return res;
|
||||
// 末尾再帰呼び出し
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* フィボナッチ数列:再帰 */
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
System.out.println("\n再帰関数の合計結果 res = " + res);
|
||||
|
||||
res = forLoopRecur(n);
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,110 @@
|
||||
/**
|
||||
* File: space_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class space_complexity {
|
||||
/* 関数 */
|
||||
static int function() {
|
||||
// 何らかの操作を実行
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 定数計算量 */
|
||||
static void constant(int n) {
|
||||
// 定数、変数、オブジェクトは O(1) 空間を占める
|
||||
final int a = 0;
|
||||
int b = 0;
|
||||
int[] nums = new int[10000];
|
||||
ListNode node = new ListNode(0);
|
||||
// ループ内の変数は O(1) 空間を占める
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// ループ内の関数は O(1) 空間を占める
|
||||
for (int i = 0; i < n; i++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
|
||||
/* 線形計算量 */
|
||||
static void linear(int n) {
|
||||
// 長さ n の配列は O(n) 空間を占める
|
||||
int[] nums = new int[n];
|
||||
// 長さ n のリストは O(n) 空間を占める
|
||||
List<ListNode> nodes = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.add(new ListNode(i));
|
||||
}
|
||||
// 長さ 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)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 二次計算量 */
|
||||
static void quadratic(int n) {
|
||||
// 行列は O(n^2) 空間を占める
|
||||
int[][] numMatrix = new int[n][n];
|
||||
// 二次元リストは O(n^2) 空間を占める
|
||||
List<List<Integer>> numList = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.add(0);
|
||||
}
|
||||
numList.add(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* 二次計算量(再帰実装) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
// 配列 nums の長さ = n, n-1, ..., 2, 1
|
||||
int[] nums = new int[n];
|
||||
System.out.println("再帰 n = " + n + " の nums の長さ = " + nums.length);
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* 指数計算量(完全二分木の構築) */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0)
|
||||
return null;
|
||||
TreeNode root = new TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,167 @@
|
||||
/**
|
||||
* File: time_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class time_complexity {
|
||||
/* 定数計算量 */
|
||||
static int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 線形計算量 */
|
||||
static int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
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 の二乗に比例
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 二次計算量(バブルソート) */
|
||||
static int bubbleSort(int[] nums) {
|
||||
int count = 0; // カウンター
|
||||
// 外側ループ:未ソート範囲は [0, i]
|
||||
for (int i = nums.length - 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;
|
||||
}
|
||||
|
||||
/* 指数計算量(ループ実装) */
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* 指数計算量(再帰実装) */
|
||||
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) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
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;
|
||||
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 階乗計算量(再帰実装) */
|
||||
static 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;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
public static void main(String[] args) {
|
||||
// n を変更して、さまざまな計算量での操作回数の変化傾向を体験可能
|
||||
int n = 8;
|
||||
System.out.println("入力データサイズ n = " + n);
|
||||
|
||||
int count = constant(n);
|
||||
System.out.println("定数計算量の操作回数 = " + count);
|
||||
|
||||
count = linear(n);
|
||||
System.out.println("線形計算量の操作回数 = " + count);
|
||||
count = arrayTraversal(new int[n]);
|
||||
System.out.println("線形計算量の操作回数(配列走査) = " + count);
|
||||
|
||||
count = quadratic(n);
|
||||
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("二次計算量の操作回数(バブルソート) = " + count);
|
||||
|
||||
count = exponential(n);
|
||||
System.out.println("指数計算量の操作回数(ループ実装) = " + count);
|
||||
count = expRecur(n);
|
||||
System.out.println("指数計算量の操作回数(再帰実装) = " + count);
|
||||
|
||||
count = logarithmic(n);
|
||||
System.out.println("対数計算量の操作回数(ループ実装) = " + count);
|
||||
count = logRecur(n);
|
||||
System.out.println("対数計算量の操作回数(再帰実装) = " + count);
|
||||
|
||||
count = linearLogRecur(n);
|
||||
System.out.println("線形対数計算量の操作回数(再帰実装) = " + count);
|
||||
|
||||
count = factorialRecur(n);
|
||||
System.out.println("階乗計算量の操作回数(再帰実装) = " + count);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: worst_best_time_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class worst_best_time_complexity {
|
||||
/* 要素 {1, 2, ..., n} をランダムにシャッフルした配列を生成 */
|
||||
static int[] randomNumbers(int n) {
|
||||
Integer[] nums = new Integer[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];
|
||||
for (int i = 0; i < n; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 配列 nums で数値1のインデックスを見つける */
|
||||
static int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// 要素1が配列の先頭にある場合、最良時間計算量 O(1) を達成
|
||||
// 要素1が配列の末尾にある場合、最悪時間計算量 O(n) を達成
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user