Bisection and prefix (C++ version)

One, two points

Binary search is a common search algorithm that can quickly locate the target value in an ordered array. The basic idea of ​​binary search is: for an ordered array, assuming that the target value to be searched is target, find the middle position mid of the array each time, and then compare target with nums[mid]. If they are equal, mid will be returned. If target If it is less than nums[mid], continue searching in the left half of the array, otherwise continue searching in the right half of the array.

The implementation of binary search needs to pay attention to the following issues:

  1. Boundary conditions: Special judgments need to be made on the left and right boundaries of the array, otherwise the array subscript out-of-bounds problem may occur.
  2. Loop condition: Generally, the left and right pointers are used to search. The loop condition is left <= right. The search ends when left > right.
  3. Calculation of the middle position: Generally, the method of left + (right - left) / 2 is used to calculate the middle position, which can avoid overflow problems.
  4. Comparison operation: If target is equal to nums[mid], find the target value and return mid; if target is less than nums[mid], search in the left half, that is, right = mid - 1; otherwise, search in the right half, that is, left = mid + 1.

The following is a simple C++ code implementation for finding the target value target in the ordered array nums:
 

int binarySearch(vector<int>& nums, int target) {
    int left = 0, right = nums.size() - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) {
            return mid;
        } else if (nums[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1;
}

 The time complexity of this algorithm is O(log n), where n is the length of the array, and the space complexity is O(1). Binary search is often used to solve ordered array search problems. For example, problems such as "searching for rotated sorted arrays" and "searching for two-dimensional matrices" in LeetCode can be solved using binary search.

2. Prefix

A prefix refers to a contiguous substring of a string starting with the first character. For example, the string "hello" is prefixed by "h", "he", "hel", and "hell". In string processing, prefix-related algorithms are often used, such as prefix sum, prefix maximum, etc.

Prefix sum refers to a new array obtained by sequentially adding the prefix elements in an array. It is usually used to quickly calculate the sum of elements in a certain interval. Taking the array nums as an example, its prefix and array prefix_sum can be calculated by the following code:
 

vector<int> prefix_sum(nums.size() + 1, 0);
for (int i = 1; i <= nums.size(); i++) {
    prefix_sum[i] = prefix_sum[i - 1] + nums[i - 1];
}

After calculating the prefix sum array, you can use prefix_sum[j+1] - prefix_sum[i] to quickly calculate the element sum of nums[i...j], where i and j are the left and right boundaries of the interval.

The prefix algorithm is a very common string algorithm that can be used to quickly calculate some interval-related statistics, such as the sum of elements, maximum value, minimum value, etc. In actual programming, mastering the prefix algorithm is very helpful to improve code efficiency and optimize program performance.

Guess you like

Origin blog.csdn.net/DonFred/article/details/129995097