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,52 +6,52 @@
#include "../utils/common.hpp"
/* Binary search (closed interval on both sides) */
/* Binary search (closed interval) */
int binarySearch(vector<int> &nums, int target) {
// Initialize closed interval [0, n-1], i.e., i, j point to the first and last elements of the array
// Initialize the closed interval [0, n-1], with i and j pointing to the first and last elements
int i = 0, j = nums.size() - 1;
// Loop, exit when the search interval is empty (empty when i > j)
// Loop until the search interval is empty (when i > j)
while (i <= j) {
int m = i + (j - i) / 2; // Calculate the midpoint index m
if (nums[m] < target) // This means target is in the interval [m+1, j]
if (nums[m] < target) // In this case, target must be in [m+1, j] if it exists
i = m + 1;
else if (nums[m] > target) // This means target is in the interval [i, m-1]
else if (nums[m] > target) // In this case, target must be in [i, m-1] if it exists
j = m - 1;
else // Found the target element, return its index
else // The target element was found; return its index
return m;
}
// Target element not found, return -1
// The target element was not found; return -1
return -1;
}
/* Binary search (left-closed right-open interval) */
/* Binary search (left-closed, right-open interval) */
int binarySearchLCRO(vector<int> &nums, int target) {
// Initialize left-closed right-open interval [0, n), i.e., i, j point to the first element and last element+1
// Initialize the left-closed, right-open interval [0, n), with i at the first element and j one past the last
int i = 0, j = nums.size();
// Loop, exit when the search interval is empty (empty when i = j)
// Loop until the search interval is empty (when i = j)
while (i < j) {
int m = i + (j - i) / 2; // Calculate the midpoint index m
if (nums[m] < target) // This means target is in the interval [m+1, j)
if (nums[m] < target) // In this case, target must be in [m+1, j) if it exists
i = m + 1;
else if (nums[m] > target) // This means target is in the interval [i, m)
else if (nums[m] > target) // In this case, target must be in [i, m) if it exists
j = m;
else // Found the target element, return its index
else // The target element was found; return its index
return m;
}
// Target element not found, return -1
// The target element was not found; return -1
return -1;
}
/* Driver Code */
/* Driver code */
int main() {
int target = 6;
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
/* Binary search (closed interval on both sides) */
/* Binary search (closed interval) */
int index = binarySearch(nums, target);
cout << "Index of target element 6 = " << index << endl;
/* Binary search (left-closed right-open interval) */
/* Binary search (left-closed, right-open interval) */
index = binarySearchLCRO(nums, target);
cout << "Index of target element 6 = " << index << endl;

View File

@@ -6,60 +6,60 @@
#include "../utils/common.hpp"
/* Binary search for insertion point (with duplicate elements) */
/* Find an insertion point using binary search (with duplicates) */
int binarySearchInsertion(const vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // Initialize closed interval [0, n-1]
int i = 0, j = nums.size() - 1; // Initialize the closed interval [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // Calculate the midpoint index m
if (nums[m] < target) {
i = m + 1; // target is in the interval [m+1, j]
i = m + 1; // Target must be in [m+1, j] if it exists
} else {
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
j = m - 1; // Continue searching [i, m-1] for the insertion point before any equal elements
}
}
// Return insertion point i
return i;
}
/* Binary search for the leftmost target */
/* Find the leftmost occurrence of target using binary search */
int binarySearchLeftEdge(vector<int> &nums, int target) {
// Equivalent to finding the insertion point of target
// This is equivalent to finding the insertion point of target
int i = binarySearchInsertion(nums, target);
// Target not found, return -1
// If target was not found, return -1
if (i == nums.size() || 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 */
int binarySearchRightEdge(vector<int> &nums, int target) {
// Convert to finding the leftmost target + 1
// Reduce the problem to finding the leftmost insertion point of target + 1
int 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
int 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 */
/* Driver code */
int main() {
// Array with duplicate elements
// Array containing duplicate elements
vector<int> nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
cout << "\nArray nums = ";
printVector(nums);
// Binary search left and right boundaries
// Find the left and right boundaries using binary search
for (int target : {6, 7}) {
int index = binarySearchLeftEdge(nums, target);
cout << "Index of leftmost element " << target << " is " << index << endl;
cout << "Leftmost occurrence of element " << target << " has index " << index << endl;
index = binarySearchRightEdge(nums, target);
cout << "Index of rightmost element " << target << " is " << index << endl;
cout << "Rightmost occurrence of element " << target << " has index " << index << endl;
}
return 0;

View File

@@ -6,60 +6,60 @@
#include "../utils/common.hpp"
/* Binary search for insertion point (no duplicate elements) */
/* Find an insertion point using binary search (no duplicates) */
int binarySearchInsertionSimple(vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // Initialize closed interval [0, n-1]
int i = 0, j = nums.size() - 1; // Initialize the closed interval [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // Calculate the midpoint index m
if (nums[m] < target) {
i = m + 1; // target is in the interval [m+1, j]
i = m + 1; // Target must be in [m+1, j] if it exists
} else if (nums[m] > target) {
j = m - 1; // target is in the interval [i, m-1]
j = m - 1; // Target must be in [i, m-1] if it exists
} else {
return m; // Found target, return insertion point m
return m; // Target was found; return insertion point m
}
}
// Target not found, return insertion point i
// Target was not found; return insertion point i
return i;
}
/* Binary search for insertion point (with duplicate elements) */
/* Find an insertion point using binary search (with duplicates) */
int binarySearchInsertion(vector<int> &nums, int target) {
int i = 0, j = nums.size() - 1; // Initialize closed interval [0, n-1]
int i = 0, j = nums.size() - 1; // Initialize the closed interval [0, n-1]
while (i <= j) {
int m = i + (j - i) / 2; // Calculate the midpoint index m
if (nums[m] < target) {
i = m + 1; // target is in the interval [m+1, j]
i = m + 1; // Target must be in [m+1, j] if it exists
} else if (nums[m] > target) {
j = m - 1; // target is in the interval [i, m-1]
j = m - 1; // Target must be in [i, m-1] if it exists
} else {
j = m - 1; // The rightmost element less than target is in the interval [i, m-1]
j = m - 1; // Continue searching [i, m-1] for the insertion point before any equal elements
}
}
// Return insertion point i
return i;
}
/* Driver Code */
/* Driver code */
int main() {
// Array without duplicate elements
// Array with no duplicate elements
vector<int> nums = {1, 3, 6, 8, 12, 15, 23, 26, 31, 35};
cout << "\nArray nums = ";
printVector(nums);
// Binary search for insertion point
// Find an insertion point using binary search
for (int target : {6, 9}) {
int index = binarySearchInsertionSimple(nums, target);
cout << "Insertion point index for element " << target << " is " << index << endl;
cout << "Element " << target << " has insertion index " << index << endl;
}
// Array with duplicate elements
// Array containing duplicate elements
nums = {1, 3, 6, 6, 6, 6, 6, 10, 12, 15};
cout << "\nArray nums = ";
printVector(nums);
// Binary search for insertion point
// Find an insertion point using binary search
for (int target : {2, 6, 20}) {
int index = binarySearchInsertion(nums, target);
cout << "Insertion point index for element " << target << " is " << index << endl;
cout << "Element " << target << " has insertion index " << index << endl;
}
return 0;

View File

@@ -6,48 +6,48 @@
#include "../utils/common.hpp"
/* Hash search (array) */
/* Hash-based search (array) */
int hashingSearchArray(unordered_map<int, int> map, int target) {
// Hash table's key: target element, value: index
// If this key does not exist in the hash table, return -1
// Hash table key: target element; value: index
// Return -1 if the hash table does not contain this key
if (map.find(target) == map.end())
return -1;
return map[target];
}
/* Hash search (linked list) */
/* Hash-based search (linked list) */
ListNode *hashingSearchLinkedList(unordered_map<int, ListNode *> map, int target) {
// Hash table key: target node value, value: node object
// Return nullptr if key does not exist in hash table
// Hash table key: target node value; value: node object
// Return nullptr if this key is absent from the hash table
if (map.find(target) == map.end())
return nullptr;
return map[target];
}
/* Driver Code */
/* Driver code */
int main() {
int target = 3;
/* Hash search (array) */
/* Hash-based search (array) */
vector<int> nums = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
// Initialize hash table
// Initialize the hash table
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
map[nums[i]] = i; // key: element, value: index
map[nums[i]] = i; // key: element; value: index
}
int index = hashingSearchArray(map, target);
cout << "Index of target element 3 = " << index << endl;
/* Hash search (linked list) */
/* Hash-based search (linked list) */
ListNode *head = vecToLinkedList(nums);
// Initialize hash table
// Initialize the hash table
unordered_map<int, ListNode *> map1;
while (head != nullptr) {
map1[head->val] = head; // key: node value, value: node
map1[head->val] = head; // key: node value; value: node
head = head->next;
}
ListNode *node = hashingSearchLinkedList(map1, target);
cout << "Node object corresponding to target node value 3 is " << node << endl;
cout << "The node object corresponding to target value 3 is " << node << endl;
return 0;
}

View File

@@ -8,13 +8,13 @@
/* Linear search (array) */
int linearSearchArray(vector<int> &nums, int target) {
// Traverse array
// Traverse the array
for (int i = 0; i < nums.size(); i++) {
// Found the target element, return its index
// The target element was found; return its index
if (nums[i] == target)
return i;
}
// Target element not found, return -1
// The target element was not found; return -1
return -1;
}
@@ -22,28 +22,28 @@ int linearSearchArray(vector<int> &nums, int target) {
ListNode *linearSearchLinkedList(ListNode *head, int target) {
// Traverse the linked list
while (head != nullptr) {
// Found the target node, return it
// The target node was found; return it
if (head->val == target)
return head;
head = head->next;
}
// Target node not found, return nullptr
// The target node was not found; return nullptr
return nullptr;
}
/* Driver Code */
/* Driver code */
int main() {
int target = 3;
/* Perform linear search in array */
/* Perform linear search on the array */
vector<int> nums = {1, 5, 3, 2, 4, 7, 5, 9, 10, 8};
int index = linearSearchArray(nums, target);
cout << "Index of target element 3 = " << index << endl;
/* Perform linear search in linked list */
/* Perform linear search on the linked list */
ListNode *head = vecToLinkedList(nums);
ListNode *node = linearSearchLinkedList(head, target);
cout << "Node object corresponding to target node value 3 is " << node << endl;
cout << "The node object corresponding to target value 3 is " << node << endl;
return 0;
}

View File

@@ -6,10 +6,10 @@
#include "../utils/common.hpp"
/* Method 1: Brute force enumeration */
/* Method 1: brute-force enumeration */
vector<int> twoSumBruteForce(vector<int> &nums, int target) {
int size = nums.size();
// Two nested loops, time complexity is O(n^2)
// Two nested loops give a time complexity of 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)
@@ -19,12 +19,12 @@ vector<int> twoSumBruteForce(vector<int> &nums, int target) {
return {};
}
/* Method 2: Auxiliary hash table */
/* Method 2: auxiliary hash table */
vector<int> twoSumHashTable(vector<int> &nums, int target) {
int size = nums.size();
// Auxiliary hash table, space complexity is O(n)
// The auxiliary hash table uses O(n) space
unordered_map<int, int> dic;
// Single loop, time complexity is O(n)
// A single loop gives a time complexity of O(n)
for (int i = 0; i < size; i++) {
if (dic.find(target - nums[i]) != dic.end()) {
return {dic[target - nums[i]], i};
@@ -34,20 +34,20 @@ vector<int> twoSumHashTable(vector<int> &nums, int target) {
return {};
}
/* Driver Code */
/* Driver code */
int main() {
// ======= Test Case =======
// ======= Test case =======
vector<int> nums = {2, 7, 11, 15};
int target = 13;
// ====== Driver Code ======
// ====== Driver code ======
// Method 1
vector<int> res = twoSumBruteForce(nums, target);
cout << "Method 1 res = ";
cout << "Method 1: res = ";
printVector(res);
// Method 2
res = twoSumHashTable(nums, target);
cout << "Method 2 res = ";
cout << "Method 2: res = ";
printVector(res);
return 0;