mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-28 15:32:03 +08:00
Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
This commit is contained in:
@@ -6,40 +6,40 @@
|
||||
|
||||
const { binarySearchInsertion } = require('./binary_search_insertion.js');
|
||||
|
||||
/* 最も左の target を二分探索 */
|
||||
/* 二分探索で最も左にある target を探す */
|
||||
function binarySearchLeftEdge(nums, target) {
|
||||
// target の挿入位置を探すのと等価
|
||||
// target の挿入位置を探すことに等しい
|
||||
const i = binarySearchInsertion(nums, target);
|
||||
// target が見つからなければ、-1 を返す
|
||||
// target が見つからなかったので、-1 を返す
|
||||
if (i === nums.length || nums[i] !== target) {
|
||||
return -1;
|
||||
}
|
||||
// target が見つかったら、インデックス i を返す
|
||||
// target が見つかったので、インデックス i を返す
|
||||
return i;
|
||||
}
|
||||
|
||||
/* 最も右の target を二分探索 */
|
||||
/* 二分探索で最も右にある target を探す */
|
||||
function binarySearchRightEdge(nums, target) {
|
||||
// 最左の target + 1 を探す問題に変換する
|
||||
// 最も左にある target + 1 を探す問題に変換する
|
||||
const i = binarySearchInsertion(nums, target + 1);
|
||||
// j は最も右の target を指し、i は target より大きい最初の要素を指す
|
||||
// j は最も右にある target を指し、i は target より大きい最初の要素を指す
|
||||
const j = i - 1;
|
||||
// target が見つからなければ、-1 を返す
|
||||
// target が見つからなかったので、-1 を返す
|
||||
if (j === -1 || nums[j] !== target) {
|
||||
return -1;
|
||||
}
|
||||
// target が見つかったら、インデックス j を返す
|
||||
// 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);
|
||||
console.log('最も左にある要素 ' + target + ' のインデックスは ' + index);
|
||||
index = binarySearchRightEdge(nums, target);
|
||||
console.log('最も右の要素 ' + target + ' のインデックスは ' + index);
|
||||
console.log('最も右にある要素 ' + target + ' のインデックスは ' + index);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user