[Algorithm Topic Breakthrough] Binary Search - Find the first and last position of an element in a sorted array (17)

Table of contents

1. Question analysis

2. Algorithm principle

3. Code writing

Write at the end:


1. Question analysis

Question link: 34. Find the first and last position of an element in a sorted array - LeetCode

This question is to find the starting position and ending position of the target value in the array.

2. Algorithm principle

If we directly use standard bisection here, if the entire array is the same number,

It will degenerate into O(N), so here we need to optimize it,

1. Start the analysis by finding the left endpoint : let x be the mid value and t be the target value.

If x < t, left = mid + 1

If x >= t, right = mid (we combine the greater than and equal situations here)

This setting is the final result when left == right.

The way we find the midpoint must be left + (right - left) / 2 (that is, the form of finding an even number)

In this way, each step will be close to the left endpoint and will not fall into an infinite loop.

2. Let’s analyze and find the right endpoint :

If x <= t, left = mid (why not use + 1 here, because if it happens to fall on the target value, will it be crossed soon?)

If x > t, right = mid - 1

In this way, when left == right, it will be the final result.

And the way we find the midpoint is left + (right - left + 1) / 2

In this way, each step will be close to the right endpoint, and you will not fall into an infinite loop.

3. Code writing

class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        if(nums.size() == 0) return {-1, -1}; // 处理越界
        int left = 0, right = nums.size() - 1, begin = 0;
        while(left < right) {
            int mid = left + (right - left) / 2;
            if(nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        
        if(nums[left] != target) return {-1, -1};
        else begin = left;

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

4. Bipartite template

When looking for the left endpoint:

        while(left < right) {

            int mid = left + (right - left) / 2;

            if(...) left = mid + 1;

            else right = mid;

        }

while (left < right) {

	int mid = left + (right - left) / 2;

	if (...) left = mid + 1;

	else right = mid;

}

When looking for the right endpoint:

        while(left < right) {

            int mid = left + (right - left + 1) / 2;

            if(...) left = mid;

            else right = mid - 1;

        }

while (left < right) {

	int mid = left + (right - left + 1) / 2;

	if (...) left = mid;

	else right = mid - 1;

}

Write at the end:

That’s the content of this article, thank you for reading.

If you feel you have gained something, you can give the blogger a like .

If there are omissions or errors in the content of the article, please send a private message to the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131804239