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:
Yudong Jin
2026-03-30 07:30:15 +08:00
committed by GitHub
parent fe6443235b
commit d7b2277d2b
1444 changed files with 83312 additions and 8363 deletions
@@ -7,39 +7,39 @@
package chapter_searching;
public class binary_search {
/* 二分探索(両閉区間) */
/* 二分探索(両閉区間) */
static int binarySearch(int[] nums, int target) {
// 両閉区間 [0, n-1] を初期化、すなわち i, j はそれぞれ配列の最初の要素と最後の要素を指す
// 両閉区間 [0, n-1] を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素を指す
int i = 0, j = nums.length - 1;
// 探索区間が空になるまでループi > j のとき空)
// ループし、探索区間が空になったら終了する(i > j 空)
while (i <= j) {
int m = i + (j - i) / 2; // 中点インデックス m を計算
if (nums[m] < target) // この状況は target 区間 [m+1, j] にあることを示す
if (nums[m] < target) // この場合、target 区間 [m+1, j] にある
i = m + 1;
else if (nums[m] > target) // この状況は target 区間 [i, m-1] にあることを示す
else if (nums[m] > target) // この場合、target 区間 [i, m-1] にある
j = m - 1;
else // 目標要素見つけたので、そのインデックスを返す
else // 目標要素見つかったらそのインデックスを返す
return m;
}
// 目標要素見つけられなかったので、-1 を返す
// 目標要素見つからなければ -1 を返す
return -1;
}
/* 二分探索(左閉右開区間) */
static int binarySearchLCRO(int[] nums, int target) {
// 左閉右開区間 [0, n) を初期化、すなわち i, j はそれぞれ配列の最初の要素と最後の要素+1を指す
// 左閉右開区間 [0, n) を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素+1を指す
int i = 0, j = nums.length;
// 探索区間が空になるまでループi = j のとき空)
// ループし、探索区間が空になったら終了する(i = j 空)
while (i < j) {
int m = i + (j - i) / 2; // 中点インデックス m を計算
if (nums[m] < target) // この状況は target 区間 [m+1, j) にあることを示す
if (nums[m] < target) // この場合、target 区間 [m+1, j) にある
i = m + 1;
else if (nums[m] > target) // この状況は target 区間 [i, m) にあることを示す
else if (nums[m] > target) // この場合、target 区間 [i, m) にある
j = m;
else // 目標要素見つけたので、そのインデックスを返す
else // 目標要素見つかったらそのインデックスを返す
return m;
}
// 目標要素見つけられなかったので、-1 を返す
// 目標要素見つからなければ -1 を返す
return -1;
}
@@ -47,12 +47,12 @@ public class binary_search {
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);
System.out.println("対象要素 6 のインデックス = " + index);
/* 二分探索(左閉右開区間) */
index = binarySearchLCRO(nums, target);
System.out.println("目標要素 6 のインデックス = " + index);
System.out.println("対象要素 6 のインデックス = " + index);
}
}
}
@@ -9,27 +9,27 @@ package chapter_searching;
public class binary_search_edge {
/* 最も左の target を二分探索 */
static int binarySearchLeftEdge(int[] nums, int target) {
// target の挿入点を見つけることと等価
// target の挿入位置を探すのと等価
int i = binary_search_insertion.binarySearchInsertion(nums, target);
// target 見つけられなかったので、-1 を返す
// target 見つからなければ、-1 を返す
if (i == nums.length || nums[i] != target) {
return -1;
}
// target 見つけたので、インデックス i を返す
// target 見つかったら、インデックス i を返す
return i;
}
/* 最も右の target を二分探索 */
static int binarySearchRightEdge(int[] nums, int target) {
// 最左の target + 1 を見つけることに変換
// 最左の target + 1 を探す問題に変換する
int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);
// j は最も右の target を指し、i は target より大きい最初の要素を指す
int j = i - 1;
// target 見つけられなかったので、-1 を返す
// target 見つからなければ、-1 を返す
if (j == -1 || nums[j] != target) {
return -1;
}
// target 見つけたので、インデックス j を返す
// target 見つかったら、インデックス j を返す
return j;
}
@@ -38,12 +38,12 @@ public class binary_search_edge {
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);
System.out.println("一番左の要素 " + target + " のインデックスは " + index);
index = binarySearchRightEdge(nums, target);
System.out.println("要素 " + target + " の最も右のインデックスは " + index);
System.out.println("一番右の要素 " + target + " のインデックスは " + index);
}
}
}
}
@@ -7,9 +7,9 @@
package chapter_searching;
class binary_search_insertion {
/* 挿入点の二分探索(重複要素なし) */
/* 二分探索で挿入位置を探す(重複要素なし) */
static int binarySearchInsertionSimple(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 両閉区間 [0, n-1] を初期化
int i = 0, j = nums.length - 1; // 両閉区間 [0, n-1] を初期化
while (i <= j) {
int m = i + (j - i) / 2; // 中点インデックス m を計算
if (nums[m] < target) {
@@ -17,16 +17,16 @@ class binary_search_insertion {
} else if (nums[m] > target) {
j = m - 1; // target は区間 [i, m-1] にある
} else {
return m; // target 見つけたので、挿入 m を返す
return m; // target 見つかったら、挿入位置 m を返す
}
}
// target 見つけられなかったので、挿入 i を返す
// target 見つからなければ、挿入位置 i を返す
return i;
}
/* 挿入点の二分探索(重複要素あり) */
/* 二分探索で挿入位置を探す(重複要素あり) */
static int binarySearchInsertion(int[] nums, int target) {
int i = 0, j = nums.length - 1; // 両閉区間 [0, n-1] を初期化
int i = 0, j = nums.length - 1; // 両閉区間 [0, n-1] を初期化
while (i <= j) {
int m = i + (j - i) / 2; // 中点インデックス m を計算
if (nums[m] < target) {
@@ -37,7 +37,7 @@ class binary_search_insertion {
j = m - 1; // target より小さい最初の要素は区間 [i, m-1] にある
}
}
// 挿入 i を返す
// 挿入位置 i を返す
return i;
}
@@ -45,19 +45,19 @@ class binary_search_insertion {
// 重複要素のない配列
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);
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);
System.out.println("要素 " + target + " の挿入位置のインデックスは " + index);
}
}
}
}
@@ -12,15 +12,15 @@ import java.util.*;
public class hashing_search {
/* ハッシュ探索(配列) */
static int hashingSearchArray(Map<Integer, Integer> map, int target) {
// ハッシュテーブルのキー: 目標要素、: インデックス
// ハッシュテーブルにこのキーが含まれていない場合、-1 を返す
// ハッシュテーブルの key: 目標要素、value: インデックス
// ハッシュテーブルにこの key がなければ -1 を返す
return map.getOrDefault(target, -1);
}
/* ハッシュ探索(連結リスト) */
static ListNode hashingSearchLinkedList(Map<Integer, ListNode> map, int target) {
// ハッシュテーブルのキー: 目標ノード値、: ノードオブジェクト
// キーがハッシュテーブルにない場合、null を返す
// ハッシュテーブルの key: 目標ノード値、value: ノードオブジェクト
// ハッシュテーブルにこの key がなければ null を返す
return map.getOrDefault(target, null);
}
@@ -32,20 +32,20 @@ public class hashing_search {
// ハッシュテーブルを初期化
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i); // キー: 要素、: インデックス
map.put(nums[i], i); // key: 要素、value: インデックス
}
int index = hashingSearchArray(map, target);
System.out.println("目標要素 3 のインデックス " + index);
System.out.println("対象要素 3 のインデックス = " + index);
/* ハッシュ探索(連結リスト) */
ListNode head = ListNode.arrToLinkedList(nums);
// ハッシュテーブルを初期化
Map<Integer, ListNode> map1 = new HashMap<>();
while (head != null) {
map1.put(head.val, head); // キー: ノード値、: ノード
map1.put(head.val, head); // key: ノード値、value: ノード
head = head.next;
}
ListNode node = hashingSearchLinkedList(map1, target);
System.out.println("目標ノード値 3 に対応するノードオブジェクトは " + node);
System.out.println("対象ノード値 3 に対応するノードオブジェクトは " + node);
}
}
}
@@ -13,38 +13,38 @@ 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 を返す
// 目標要素見つからなければ -1 を返す
return -1;
}
/* 線形探索(連結リスト) */
static ListNode linearSearchLinkedList(ListNode head, int target) {
// リストを走査
// 連結リストを走査
while (head != null) {
// 目標ノード見つけたので、それを返す
// 対象ノード見つかったら、それを返す
if (head.val == target)
return head;
head = head.next;
}
// 目標ノードが見つからない場合null を返す
// 対象ノードが見つからない場合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);
System.out.println("対象要素 3 のインデックス = " + index);
/* 連結リストで線形探索を行 */
/* 連結リストで線形探索を行 */
ListNode head = ListNode.arrToLinkedList(nums);
ListNode node = linearSearchLinkedList(head, target);
System.out.println("目標ノード値 3 に対応するノードオブジェクトは " + node);
System.out.println("対象ノード値 3 に対応するノードオブジェクトは " + node);
}
}
}
+12 -12
View File
@@ -9,10 +9,10 @@ package chapter_searching;
import java.util.*;
public class two_sum {
/* 方法一: 暴力列挙 */
/* 方法 1:総当たり列挙 */
static int[] twoSumBruteForce(int[] nums, int target) {
int size = nums.length;
// 重ループ、時間計算量は O(n^2)
// 2重ループのため、時間計算量は 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)
@@ -22,12 +22,12 @@ public class two_sum {
return new int[0];
}
/* 方法二: 補助ハッシュテーブル */
/* 方法 2補助ハッシュテーブル */
static int[] twoSumHashTable(int[] nums, int target) {
int size = nums.length;
// 補助ハッシュテーブル、空間計算量は O(n)
// 補助ハッシュテーブルを使用し、空間計算量は O(n)
Map<Integer, Integer> dic = new HashMap<>();
// 単一ループ、時間計算量は O(n)
// 単一ループ、時間計算量は O(n)
for (int i = 0; i < size; i++) {
if (dic.containsKey(target - nums[i])) {
return new int[] { dic.get(target - nums[i]), i };
@@ -38,16 +38,16 @@ public class two_sum {
}
public static void main(String[] args) {
// ======= テストケース =======
// ======= Test Case =======
int[] nums = { 2, 7, 11, 15 };
int target = 13;
// ====== ドライバーコード ======
// 方法
// ====== Driver Code ======
// 方法 1
int[] res = twoSumBruteForce(nums, target);
System.out.println("方法 res = " + Arrays.toString(res));
// 方法
System.out.println("方法1 res = " + Arrays.toString(res));
// 方法 2
res = twoSumHashTable(nums, target);
System.out.println("方法 res = " + Arrays.toString(res));
System.out.println("方法2 res = " + Arrays.toString(res));
}
}
}