Problem-solving ideas and codes for searching rotated ordered arrays and dealing with repeated elements (binary search)

introduce:

Searching through rotated sorted arrays and dealing with duplicate elements are two common problems in solving algorithmic problems. This blog post will introduce the problem-solving ideas and code implementation of these two problems in detail, and explain the key steps.

Table of contents:

Search Rotation Ordered Array Problem
Description Problem
Solving Ideas
Sample Code
Dealing with Repeated Element Search Rotation Ordered Array Problem
Description
Problem Solving Ideas
Sample Code

Search for rotated sorted array problems:

Title description:

Suppose a sorted array is rotated around some unknown pivot (for example, 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Given a target value to search for. If the target value is found in the array, its index is returned; otherwise -1 is returned. It can be assumed that there are no duplicate elements in the array.

Problem-solving ideas:

Use the idea of ​​binary search to solve the problem. The key is to scope the search at each step. By comparing the size relationship between the middle element and the starting element, it is judged whether the rotation point is on the left or the right, and then the search range is updated until the target value is found or the search range is empty.

Solution:

Define two pointers left and right to point to the start position and end position of the array respectively.
Use a loop to iterate when left <= right.
In each iteration, the index mid of the middle element is calculated and compared with the target value.
If the middle element is equal to the target value, return mid directly.
Otherwise, judge whether the rotation point is between left to mid or between mid+1 and right according to the size relationship between the middle element and the start element.
If the rotation point is between left and mid, and the target value is within that range, update right to mid-1.
Otherwise, update left to mid+1.
If the target value is still not found at the end of the loop, it means that the target value does not exist in the array, and -1 is returned.

Sample code:

int searchInRotatedSortedArray(vector<int>& nums, int target) {
    
    
    int left = 0;
    int right = nums.size() - 1;

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

        if (nums[mid] == target) {
    
    
            return mid;
        }

        if (nums[left] <= nums[mid]) {
    
    
            if (target >= nums[left] && target < nums[mid]) {
    
    
                right = mid - 1;
            } else {
    
    
                left = mid + 1;
            }
        } else {
    
    
            if (target > nums[mid] && target <= nums[right]) {
    
    
                left = mid + 1;
            } else {
    
    
                right = mid - 1;
            }
        }
    }

    return -1;
}

The search rotated sorted array problem dealing with duplicate elements:

Title description:

Suppose a sorted array is rotated around some unknown pivot (for example, 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Given a target value to search for. Returns true if the target value is found in the array; otherwise returns false. It can be assumed that there are duplicate elements in the array.

Problem-solving ideas:

In the case of dealing with repeated elements, we need to handle the special case where the middle element is equal to the start element, and shift the start position one bit to the right. Other parts are similar to the previous solution.

If repeated elements are allowed, it will affect the runtime complexity and require modification of the original solution. Where duplicate elements are allowed, it is possible that the start, middle, and end elements of an array may be equal. This makes it impossible to determine the interval in which the rotation point lies.

Solution

To solve this problem, we need to compare the middle element with the starting element, and consider the following three cases:

If the middle element is larger than the start element, then the rotation point is on the right, and we can set the start position to be the middle plus 1.
If the middle element is smaller than the start element, it means that the rotation point is on the left, and we can set the end position to be the middle position minus 1.
If the middle element is equal to the start element and the interval where the rotation point is located cannot be determined, the start position can be moved one bit to the right.

Sample code:

bool searchInRotatedSortedArray(vector<int>& nums, int target) {
    
    
    int left = 0;
    int right = nums.size() - 1;

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

        if (nums[mid] == target) {
    
    
            return true;
        }

        if (nums[left] == nums[mid]) {
    
    
            left++;
        } else if (nums[left] < nums[mid]) {
    
    
            if (target >= nums[left] && target < nums[mid]) {
    
    
                right = mid - 1;
            } else {
    
    
                left = mid + 1;
            }
        } else {
    
    
            if (target > nums[mid] && target <= nums[right]) {
    
    
                left = mid + 1;
            } else {
    
    
                right = mid - 1;
            }
        }
    }

    return false;
}

in conclusion:

This blog post introduces in detail the problem-solving ideas and code implementation of searching and rotating ordered arrays and dealing with repeated elements. By using the idea of ​​binary search, we can efficiently search for the target value in the rotated sorted array. At the same time, in the case of dealing with repeated elements, special attention needs to be paid to the comparison between the middle element and the starting element. I hope this article can help readers understand and solve these two problems, and use them flexibly in actual programming. If you want to dig into more details, please refer to relevant algorithm books and sources. Good luck with your algorithmic problems!

Guess you like

Origin blog.csdn.net/qq_46017342/article/details/131744980