Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -6,40 +6,40 @@
const { binarySearchInsertion } = require('./binary_search_insertion.js');
/* Binary search for the leftmost target */
/* Find the leftmost occurrence of target using binary search */
function binarySearchLeftEdge(nums, target) {
// Equivalent to finding the insertion point of target
// This is equivalent to finding the insertion point of target
const i = binarySearchInsertion(nums, target);
// Target not found, return -1
// If target was not found, return -1
if (i === nums.length || nums[i] !== target) {
return -1;
}
// Found target, return index i
// Target was found; return index i
return i;
}
/* Binary search for the rightmost target */
/* Find the rightmost occurrence of target using binary search */
function binarySearchRightEdge(nums, target) {
// Convert to finding the leftmost target + 1
// Reduce the problem to finding the leftmost insertion point of target + 1
const i = binarySearchInsertion(nums, target + 1);
// j points to the rightmost target, i points to the first element greater than target
// j points to the rightmost target, while i points to the first element greater than target
const j = i - 1;
// Target not found, return -1
// If target was not found, return -1
if (j === -1 || nums[j] !== target) {
return -1;
}
// Found target, return index j
// Target was found; return index j
return j;
}
/* Driver Code */
// Array with duplicate elements
/* Driver code */
// Array containing duplicate elements
const nums = [1, 3, 6, 6, 6, 6, 6, 10, 12, 15];
console.log('\nArray nums = ' + nums);
// Binary search left and right boundaries
// Find the left and right boundaries using binary search
for (const target of [6, 7]) {
let index = binarySearchLeftEdge(nums, target);
console.log('Leftmost element ' + target + ' has index ' + index);
console.log('Leftmost occurrence of ' + target + ' has index ' + index);
index = binarySearchRightEdge(nums, target);
console.log('Rightmost element ' + target + ' has index ' + index);
console.log('Rightmost occurrence of ' + target + ' has index ' + index);
}