mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-17 14:10:57 +00:00
build
This commit is contained in:
@@ -76,7 +76,23 @@ The code is as follows:
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_search.cpp"
|
||||
[class]{}-[func]{binarySearch}
|
||||
/* Binary search (double closed interval) */
|
||||
int binarySearch(vector<int> &nums, int target) {
|
||||
// Initialize double closed interval [0, n-1], i.e., i, j point to the first element and last element of the array respectively
|
||||
int i = 0, j = nums.size() - 1;
|
||||
// Loop until the search interval is empty (when i > j, it is empty)
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate midpoint index m
|
||||
if (nums[m] < target) // This situation indicates that target is in the interval [m+1, j]
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // This situation indicates that target is in the interval [i, m-1]
|
||||
j = m - 1;
|
||||
else // Found the target element, thus return its index
|
||||
return m;
|
||||
}
|
||||
// Did not find the target element, thus return -1
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
@@ -199,7 +215,23 @@ We can implement a binary search algorithm with the same functionality based on
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_search.cpp"
|
||||
[class]{}-[func]{binarySearchLCRO}
|
||||
/* 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 the last element +1 of the array respectively
|
||||
int i = 0, j = nums.size();
|
||||
// Loop until the search interval is empty (when i = j, it is empty)
|
||||
while (i < j) {
|
||||
int m = i + (j - i) / 2; // Calculate midpoint index m
|
||||
if (nums[m] < target) // This situation indicates that target is in the interval [m+1, j)
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // This situation indicates that target is in the interval [i, m)
|
||||
j = m;
|
||||
else // Found the target element, thus return its index
|
||||
return m;
|
||||
}
|
||||
// Did not find the target element, thus return -1
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
@@ -36,7 +36,17 @@ In these cases, simply return $-1$. The code is as follows:
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_search_edge.cpp"
|
||||
[class]{}-[func]{binarySearchLeftEdge}
|
||||
/* Binary search for the leftmost target */
|
||||
int binarySearchLeftEdge(vector<int> &nums, int target) {
|
||||
// Equivalent to finding the insertion point of target
|
||||
int i = binarySearchInsertion(nums, target);
|
||||
// Did not find target, thus return -1
|
||||
if (i == nums.size() || nums[i] != target) {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index i
|
||||
return i;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
@@ -158,7 +168,19 @@ Please note, the insertion point returned is $i$, therefore, it should be subtra
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_search_edge.cpp"
|
||||
[class]{}-[func]{binarySearchRightEdge}
|
||||
/* Binary search for the rightmost target */
|
||||
int binarySearchRightEdge(vector<int> &nums, int target) {
|
||||
// Convert to finding the leftmost target + 1
|
||||
int i = binarySearchInsertion(nums, target + 1);
|
||||
// j points to the rightmost target, i points to the first element greater than target
|
||||
int j = i - 1;
|
||||
// Did not find target, thus return -1
|
||||
if (j == -1 || nums[j] != target) {
|
||||
return -1;
|
||||
}
|
||||
// Found target, return index j
|
||||
return j;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
@@ -49,7 +49,22 @@ Therefore, at the end of the binary, it is certain that: $i$ points to the first
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_search_insertion.cpp"
|
||||
[class]{}-[func]{binarySearchInsertionSimple}
|
||||
/* Binary search for insertion point (no duplicate elements) */
|
||||
int binarySearchInsertionSimple(vector<int> &nums, int target) {
|
||||
int i = 0, j = nums.size() - 1; // Initialize double closed interval [0, n-1]
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate midpoint index m
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // Target is in interval [m+1, j]
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // Target is in interval [i, m-1]
|
||||
} else {
|
||||
return m; // Found target, return insertion point m
|
||||
}
|
||||
}
|
||||
// Did not find target, return insertion point i
|
||||
return i;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
@@ -216,7 +231,22 @@ Even so, we can still keep the conditions expanded, as their logic is clearer an
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_search_insertion.cpp"
|
||||
[class]{}-[func]{binarySearchInsertion}
|
||||
/* Binary search for insertion point (with duplicate elements) */
|
||||
int binarySearchInsertion(vector<int> &nums, int target) {
|
||||
int i = 0, j = nums.size() - 1; // Initialize double closed interval [0, n-1]
|
||||
while (i <= j) {
|
||||
int m = i + (j - i) / 2; // Calculate midpoint index m
|
||||
if (nums[m] < target) {
|
||||
i = m + 1; // Target is in interval [m+1, j]
|
||||
} else if (nums[m] > target) {
|
||||
j = m - 1; // Target is in interval [i, m-1]
|
||||
} else {
|
||||
j = m - 1; // First element less than target is in interval [i, m-1]
|
||||
}
|
||||
}
|
||||
// Return insertion point i
|
||||
return i;
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
@@ -36,7 +36,18 @@ The code is shown below:
|
||||
=== "C++"
|
||||
|
||||
```cpp title="two_sum.cpp"
|
||||
[class]{}-[func]{twoSumBruteForce}
|
||||
/* Method one: Brute force enumeration */
|
||||
vector<int> twoSumBruteForce(vector<int> &nums, int target) {
|
||||
int size = nums.size();
|
||||
// Two-layer loop, time complexity is 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)
|
||||
return {i, j};
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
@@ -126,7 +137,7 @@ This method has a time complexity of $O(n^2)$ and a space complexity of $O(1)$,
|
||||
|
||||
## 10.4.2 Hash search: trading space for time
|
||||
|
||||
Consider using a hash table, with key-value pairs being the array elements and their indices, respectively. Loop through the array, performing the steps shown in the figures below each round.
|
||||
Consider using a hash table, with key-value pairs being the array elements and their indices, respectively. Loop through the array, performing the steps shown in Figure 10-10 each round.
|
||||
|
||||
1. Check if the number `target - nums[i]` is in the hash table. If so, directly return the indices of these two elements.
|
||||
2. Add the key-value pair `nums[i]` and index `i` to the hash table.
|
||||
@@ -162,7 +173,20 @@ The implementation code is shown below, requiring only a single loop:
|
||||
=== "C++"
|
||||
|
||||
```cpp title="two_sum.cpp"
|
||||
[class]{}-[func]{twoSumHashTable}
|
||||
/* Method two: Auxiliary hash table */
|
||||
vector<int> twoSumHashTable(vector<int> &nums, int target) {
|
||||
int size = nums.size();
|
||||
// Auxiliary hash table, space complexity is O(n)
|
||||
unordered_map<int, int> dic;
|
||||
// Single-layer loop, time complexity is O(n)
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (dic.find(target - nums[i]) != dic.end()) {
|
||||
return {dic[target - nums[i]], i};
|
||||
}
|
||||
dic.emplace(nums[i], i);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
Reference in New Issue
Block a user