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:
Ikko Eltociear Ashimine
2025-10-17 06:04:43 +09:00
committed by GitHub
parent 2487a27036
commit 954c45864b
886 changed files with 33569 additions and 0 deletions

View File

@ -0,0 +1,58 @@
/**
* File: binary_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
public class binary_search {
/* 二分探索(両端閉区間) */
static int binarySearch(int[] nums, int target) {
// 両端閉区間 [0, n-1] を初期化、すなわち i, j はそれぞれ配列の最初の要素と最後の要素を指す
int i = 0, j = nums.length - 1;
// 探索区間が空になるまでループi > j のとき空)
while (i <= j) {
int m = i + (j - i) / 2; // 中点インデックス m を計算
if (nums[m] < target) // この状況は target が区間 [m+1, j] にあることを示す
i = m + 1;
else if (nums[m] > target) // この状況は target が区間 [i, m-1] にあることを示す
j = m - 1;
else // 目標要素を見つけたので、そのインデックスを返す
return m;
}
// 目標要素を見つけられなかったので、-1 を返す
return -1;
}
/* 二分探索(左閉右開区間) */
static int binarySearchLCRO(int[] nums, int target) {
// 左閉右開区間 [0, n) を初期化、すなわち i, j はそれぞれ配列の最初の要素と最後の要素+1を指す
int i = 0, j = nums.length;
// 探索区間が空になるまでループi = j のとき空)
while (i < j) {
int m = i + (j - i) / 2; // 中点インデックス m を計算
if (nums[m] < target) // この状況は target が区間 [m+1, j) にあることを示す
i = m + 1;
else if (nums[m] > target) // この状況は target が区間 [i, m) にあることを示す
j = m;
else // 目標要素を見つけたので、そのインデックスを返す
return m;
}
// 目標要素を見つけられなかったので、-1 を返す
return -1;
}
public static void main(String[] args) {
int target = 6;
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
/* 二分探索(両端閉区間) */
int index = binarySearch(nums, target);
System.out.println("目標要素 6 のインデックス = " + index);
/* 二分探索(左閉右開区間) */
index = binarySearchLCRO(nums, target);
System.out.println("目標要素 6 のインデックス = " + index);
}
}

View File

@ -0,0 +1,49 @@
/**
* File: binary_search_edge.java
* Created Time: 2023-08-04
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
public class binary_search_edge {
/* 最も左の target を二分探索 */
static int binarySearchLeftEdge(int[] nums, int target) {
// target の挿入点を見つけることと等価
int i = binary_search_insertion.binarySearchInsertion(nums, target);
// target を見つけられなかったので、-1 を返す
if (i == nums.length || nums[i] != target) {
return -1;
}
// target を見つけたので、インデックス i を返す
return i;
}
/* 最も右の target を二分探索 */
static int binarySearchRightEdge(int[] nums, int target) {
// 最も左の target + 1 を見つけることに変換
int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
// j は最も右の target を指し、i は target より大きい最初の要素を指す
int j = i - 1;
// target を見つけられなかったので、-1 を返す
if (j == -1 || nums[j] != target) {
return -1;
}
// target を見つけたので、インデックス j を返す
return j;
}
public static void main(String[] args) {
// 重複要素を含む配列
int[] nums = { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
System.out.println("\n配列 nums = " + java.util.Arrays.toString(nums));
// 左右の境界を二分探索
for (int target : new int[] { 6, 7 }) {
int index = binarySearchLeftEdge(nums, target);
System.out.println("要素 " + target + " の最も左のインデックスは " + index);
index = binarySearchRightEdge(nums, target);
System.out.println("要素 " + target + " の最も右のインデックスは " + index);
}
}
}

View File

@ -0,0 +1,63 @@
/**
* File: binary_search_insertion.java
* Created Time: 2023-08-04
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
class binary_search_insertion {
/* 挿入点の二分探索(重複要素なし) */
static int binarySearchInsertionSimple(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 両端閉区間 [0, n-1] を初期化
while (i <= j) {
int m = i + (j - i) / 2; // 中点インデックス m を計算
if (nums[m] < target) {
i = m + 1; // target は区間 [m+1, j] にある
} else if (nums[m] > target) {
j = m - 1; // target は区間 [i, m-1] にある
} else {
return m; // target を見つけたので、挿入点 m を返す
}
}
// target を見つけられなかったので、挿入点 i を返す
return i;
}
/* 挿入点の二分探索(重複要素あり) */
static int binarySearchInsertion(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 両端閉区間 [0, n-1] を初期化
while (i <= j) {
int m = i + (j - i) / 2; // 中点インデックス m を計算
if (nums[m] < target) {
i = m + 1; // target は区間 [m+1, j] にある
} else if (nums[m] > target) {
j = m - 1; // target は区間 [i, m-1] にある
} else {
j = m - 1; // target より小さい最初の要素は区間 [i, m-1] にある
}
}
// 挿入点 i を返す
return i;
}
public static void main(String[] args) {
// 重複要素のない配列
int[] nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 };
System.out.println("\n配列 nums = " + java.util.Arrays.toString(nums));
// 挿入点の二分探索
for (int target : new int[] { 6, 9 }) {
int index = binarySearchInsertionSimple(nums, target);
System.out.println("要素 " + target + " の挿入点インデックスは " + index);
}
// 重複要素のある配列
nums = new int[] { 1, 3, 6, 6, 6, 6, 6, 10, 12, 15 };
System.out.println("\n配列 nums = " + java.util.Arrays.toString(nums));
// 挿入点の二分探索
for (int target : new int[] { 2, 6, 20 }) {
int index = binarySearchInsertion(nums, target);
System.out.println("要素 " + target + " の挿入点インデックスは " + index);
}
}
}

View File

@ -0,0 +1,51 @@
/**
* File: hashing_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import utils.*;
import java.util.*;
public class hashing_search {
/* ハッシュ探索(配列) */
static int hashingSearchArray(Map<Integer, Integer> map, int target) {
// ハッシュテーブルのキー: 目標要素、値: インデックス
// ハッシュテーブルにこのキーが含まれていない場合、-1 を返す
return map.getOrDefault(target, -1);
}
/* ハッシュ探索(連結リスト) */
static ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
// ハッシュテーブルのキー: 目標ノードの値、値: ノードオブジェクト
// キーがハッシュテーブルにない場合、null を返す
return map.getOrDefault(target, null);
}
public static void main(String[] args) {
int target = 3;
/* ハッシュ探索(配列) */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
// ハッシュテーブルを初期化
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i); // キー: 要素、値: インデックス
}
int index = hashingSearchArray(map, target);
System.out.println("目標要素 3 のインデックスは " + index);
/* ハッシュ探索(連結リスト) */
ListNode head = ListNode.arrToLinkedList(nums);
// ハッシュテーブルを初期化
Map<Integer, ListNode> map1 = new HashMap<>();
while (head != null) {
map1.put(head.val, head); // キー: ノードの値、値: ノード
head = head.next;
}
ListNode node = hashingSearchLinkedList(map1, target);
System.out.println("目標ノード値 3 に対応するノードオブジェクトは " + node);
}
}

View File

@ -0,0 +1,50 @@
/**
* File: linear_search.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import utils.*;
public class linear_search {
/* 線形探索(配列) */
static int linearSearchArray(int[] nums, int target) {
// 配列を走査
for (int i = 0; i < nums.length; i++) {
// 目標要素を見つけたので、そのインデックスを返す
if (nums[i] == target)
return i;
}
// 目標要素を見つけられなかったので、-1 を返す
return -1;
}
/* 線形探索(連結リスト) */
static ListNode linearSearchLinkedList(ListNode head, int target) {
// リストを走査
while (head != null) {
// 目標ノードを見つけたので、それを返す
if (head.val == target)
return head;
head = head.next;
}
// 目標ードが見つからない場合、null を返す
return null;
}
public static void main(String[] args) {
int target = 3;
/* 配列で線形探索を実行 */
int[] nums = { 1, 5, 3, 2, 4, 7, 5, 9, 10, 8 };
int index = linearSearchArray(nums, target);
System.out.println("目標要素 3 のインデックスは " + index);
/* 連結リストで線形探索を実行 */
ListNode head = ListNode.arrToLinkedList(nums);
ListNode node = linearSearchLinkedList(head, target);
System.out.println("目標ノード値 3 に対応するノードオブジェクトは " + node);
}
}

View File

@ -0,0 +1,53 @@
/**
* File: two_sum.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_searching;
import java.util.*;
public class two_sum {
/* 方法一: 暴力列挙 */
static int[] twoSumBruteForce(int[] nums, int target) {
int size = nums.length;
// 二重ループ、時間計算量は O(n^2)
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (nums[i] + nums[j] == target)
return new int[] { i, j };
}
}
return new int[0];
}
/* 方法二: 補助ハッシュテーブル */
static int[] twoSumHashTable(int[] nums, int target) {
int size = nums.length;
// 補助ハッシュテーブル、空間計算量は O(n)
Map<Integer, Integer> dic = new HashMap<>();
// 単一層ループ、時間計算量は O(n)
for (int i = 0; i < size; i++) {
if (dic.containsKey(target - nums[i])) {
return new int[] { dic.get(target - nums[i]), i };
}
dic.put(nums[i], i);
}
return new int[0];
}
public static void main(String[] args) {
// ======= テストケース =======
int[] nums = { 2, 7, 11, 15 };
int target = 13;
// ====== ドライバーコード ======
// 方法一
int[] res = twoSumBruteForce(nums, target);
System.out.println("方法一 res = " + Arrays.toString(res));
// 方法二
res = twoSumHashTable(nums, target);
System.out.println("方法二 res = " + Arrays.toString(res));
}
}