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

View File

@@ -0,0 +1,60 @@
/**
* File: binary_search.js
* Created Time: 2022-12-22
* Author: JoseHung (szhong@link.cuhk.edu.hk)
*/
/* 二分探索(両閉区間) */
function binarySearch(nums, target) {
// 両閉区間 [0, n-1] を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素を指す
let i = 0,
j = nums.length - 1;
// ループし、探索区間が空になったら終了するi > j で空)
while (i <= j) {
// 中点インデックス `m` を計算し、`parseInt()` で切り捨てる
const m = parseInt(i + (j - i) / 2);
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;
}
/* 二分探索(左閉右開区間) */
function binarySearchLCRO(nums, target) {
// 左閉右開区間 [0, n) を初期化する。つまり i, j はそれぞれ配列の先頭要素と末尾要素+1を指す
let i = 0,
j = nums.length;
// ループし、探索区間が空になったら終了するi = j で空)
while (i < j) {
// 中点インデックス `m` を計算し、`parseInt()` で切り捨てる
const m = parseInt(i + (j - i) / 2);
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;
}
/* Driver Code */
const target = 6;
const nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
/* 二分探索(両閉区間) */
let index = binarySearch(nums, target);
console.log('目標要素 6 のインデックス = ' + index);
/* 二分探索(左閉右開区間) */
index = binarySearchLCRO(nums, target);
console.log('目標要素 6 のインデックス = ' + index);

View File

@@ -0,0 +1,45 @@
/**
* File: binary_search_edge.js
* Created Time: 2023-08-22
* Author: Gaofer Chou (gaofer-chou@qq.com)
*/
const { binarySearchInsertion } = require('./binary_search_insertion.js');
/* 最も左の target を二分探索 */
function binarySearchLeftEdge(nums, target) {
// target の挿入位置を探すのと等価
const i = binarySearchInsertion(nums, target);
// target が見つからなければ、-1 を返す
if (i === nums.length || nums[i] !== target) {
return -1;
}
// target が見つかったら、インデックス i を返す
return i;
}
/* 最も右の target を二分探索 */
function binarySearchRightEdge(nums, target) {
// 最左の target + 1 を探す問題に変換する
const i = binarySearchInsertion(nums, target + 1);
// j は最も右の target を指し、i は target より大きい最初の要素を指す
const j = i - 1;
// target が見つからなければ、-1 を返す
if (j === -1 || nums[j] !== target) {
return -1;
}
// target が見つかったら、インデックス j を返す
return j;
}
/* Driver Code */
// 重複要素を含む配列
const nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
console.log('\n配列 nums = ' + nums);
// 二分探索で左端と右端を探す
for (const target of [6, 7]) {
let index = binarySearchLeftEdge(nums, target);
console.log('最も左の要素 ' + target + ' のインデックスは ' + index);
index = binarySearchRightEdge(nums, target);
console.log('最も右の要素 ' + target + ' のインデックスは ' + index);
}

View File

@@ -0,0 +1,64 @@
/**
* File: binary_search_insertion.js
* Created Time: 2023-08-22
* Author: Gaofer Chou (gaofer-chou@qq.com)
*/
/* 二分探索で挿入位置を探す(重複要素なし) */
function binarySearchInsertionSimple(nums, target) {
let i = 0,
j = nums.length - 1; // 両閉区間 [0, n-1] を初期化
while (i <= j) {
const m = Math.floor(i + (j - i) / 2); // 中点インデックス m を計算し、Math.floor() で切り捨てる
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;
}
/* 二分探索で挿入位置を探す(重複要素あり) */
function binarySearchInsertion(nums, target) {
let i = 0,
j = nums.length - 1; // 両閉区間 [0, n-1] を初期化
while (i <= j) {
const m = Math.floor(i + (j - i) / 2); // 中点インデックス m を計算し、Math.floor() で切り捨てる
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;
}
/* Driver Code */
// 重複要素のない配列
let nums = [1, 3, 6, 8, 12, 15, 23, 26, 31, 35];
console.log('\n配列 nums = ' + nums);
// 二分探索で挿入位置を探す
for (const target of [6, 9]) {
const index = binarySearchInsertionSimple(nums, target);
console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index);
}
// 重複要素を含む配列
nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
console.log('\n配列 nums = ' + nums);
// 二分探索で挿入位置を探す
for (const target of [2, 6, 20]) {
const index = binarySearchInsertion(nums, target);
console.log('要素 ' + target + ' の挿入位置のインデックスは ' + index);
}
module.exports = {
binarySearchInsertion,
};

View File

@@ -0,0 +1,45 @@
/**
* File: hashing_search.js
* Created Time: 2022-12-29
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
const { arrToLinkedList } = require('../modules/ListNode');
/* ハッシュ探索(配列) */
function hashingSearchArray(map, target) {
// ハッシュテーブルの key: 目標要素、value: インデックス
// ハッシュテーブルにこの key がなければ -1 を返す
return map.has(target) ? map.get(target) : -1;
}
/* ハッシュ探索(連結リスト) */
function hashingSearchLinkedList(map, target) {
// ハッシュテーブルの key: 目標ード値、value: ノードオブジェクト
// ハッシュテーブルにこの key がなければ null を返す
return map.has(target) ? map.get(target) : null;
}
/* Driver Code */
const target = 3;
/* ハッシュ探索(配列) */
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
// ハッシュテーブルを初期化
const map = new Map();
for (let i = 0; i < nums.length; i++) {
map.set(nums[i], i); // key: 要素、value: インデックス
}
const index = hashingSearchArray(map, target);
console.log('目標要素 3 のインデックス = ' + index);
/* ハッシュ探索(連結リスト) */
let head = arrToLinkedList(nums);
// ハッシュテーブルを初期化
const map1 = new Map();
while (head != null) {
map1.set(head.val, head); // key: ード値、value: ノード
head = head.next;
}
const node = hashingSearchLinkedList(map1, target);
console.log('目標ノード値 3 に対応するノードオブジェクトは', node);

View File

@@ -0,0 +1,47 @@
/**
* File: linear_search.js
* Created Time: 2022-12-22
* Author: JoseHung (szhong@link.cuhk.edu.hk)
*/
const { ListNode, arrToLinkedList } = require('../modules/ListNode');
/* 線形探索(配列) */
function linearSearchArray(nums, target) {
// 配列を走査
for (let i = 0; i < nums.length; i++) {
// 目標要素が見つかったらそのインデックスを返す
if (nums[i] === target) {
return i;
}
}
// 目標要素が見つからなければ -1 を返す
return -1;
}
/* 線形探索(連結リスト) */
function linearSearchLinkedList(head, target) {
// 連結リストを走査
while (head) {
// 対象ノードが見つかったら、それを返す
if (head.val === target) {
return head;
}
head = head.next;
}
// 対象ノードが見つからない場合は null を返す
return null;
}
/* Driver Code */
const target = 3;
/* 配列で線形探索を行う */
const nums = [1, 5, 3, 2, 4, 7, 5, 9, 10, 8];
const index = linearSearchArray(nums, target);
console.log('目標要素 3 のインデックス = ' + index);
/* 連結リストで線形探索を行う */
const head = arrToLinkedList(nums);
const node = linearSearchLinkedList(head, target);
console.log('目標ノード値 3 に対応するノードオブジェクトは ', node);

View File

@@ -0,0 +1,46 @@
/**
* File: two_sum.js
* Created Time: 2022-12-15
* Author: gyt95 (gytkwan@gmail.com)
*/
/* 方法 1総当たり列挙 */
function twoSumBruteForce(nums, target) {
const n = nums.length;
// 2重ループのため、時間計算量は O(n^2)
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
}
return [];
}
/* 方法 2補助ハッシュテーブル */
function twoSumHashTable(nums, target) {
// 補助ハッシュテーブルを使用し、空間計算量は O(n)
let m = {};
// 単一ループで、時間計算量は O(n)
for (let i = 0; i < nums.length; i++) {
if (m[target - nums[i]] !== undefined) {
return [m[target - nums[i]], i];
} else {
m[nums[i]] = i;
}
}
return [];
}
/* Driver Code */
// 方法 1
const nums = [2, 7, 11, 15],
target = 13;
let res = twoSumBruteForce(nums, target);
console.log('方法1 res = ', res);
// 方法 2
res = twoSumHashTable(nums, target);
console.log('方法2 res = ', res);