C ++ algorithm: find elements in the sorted array is the first and last positions ------ binary search pointer bis +

topic:

Nums Given an array of integers arranged in ascending order, according to the target and one target value. Identify a given target start and end positions in the array. Your time complexity of the algorithm must be O (log n) level. If the target is not present in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1, -1]

method one:

Using the binary search method to find the position of a matching, and a method using double pointer to find upper and lower bounds

The method of using two :( recursive binary search method to make the time complexity of achieving the O (log n))

Using recursive binary search method, when nums [mid] == using the binary search method for finding a first matching element and the last element of a matching target position when

Code:

#if 0
//方法一:先使用二分查找法查找出一个匹配的位置,然后使用双指针方法寻找上界和下界
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int low = 0, hight = nums.size()-1;
        int mid;
        int down, up;
        
        if(hight < 0)
            return {-1,-1};
        else
        {
            while(low <= hight)
            {
                mid = (hight + low)/2;
                if(nums[mid] < target)
                    low = mid + 1;
                else if(nums[mid] > target)
                    hight = mid - 1;
                else
                {
                    //找到一个匹配的后,查找其范围
                    //找下界
                    down = mid;
                    while((down > 0) && nums[down] == nums[down-1])
                        down -= 1;
                    up = mid;
                    while((up < nums.size()-1) && nums[up] == nums[up+1])
                        up++;
                    return {down, up};
                }
            }
            return {-1,-1};
        }
    }
};

#endif

#if 1
//方法二:使用递归实现二分查找法,当nums[mid] == target时使用二分查找法寻找第一个匹配的元素和最后一个匹配的元素位置
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        int a = nums.size(), b = -1;
        binary_search(nums, target, 0, nums.size()-1, a, b);
        
        if(a < nums.size() && b > -1)
            return {a, b};
        else
            return {-1,-1};
    }
    
private:
    void binary_search(vector<int>& nums, int target, int begin, int end, int &a, int &b)  //a记录下边界,b记录上边界
    {
        if(begin > end) //  终止条件
            return ;
        
        int mid = (begin + end)/2;
        if(nums[mid] == target)
        {
            if(a > mid)
                a = mid;
            if(b < mid)
                b = mid;
            binary_search(nums, target, begin, mid-1, a, b);  //寻找下边界
            binary_search(nums, target, mid+1, end, a, b);    //寻找上边界
        }
        else if(nums[mid] > target)  //普通二分查找
            binary_search(nums, target, begin, mid-1, a, b);
        else        //普通二分查找
            binary_search(nums, target, mid+1, end, a, b);
    }
};
#endif

Guess you like

Origin blog.csdn.net/qq_31820761/article/details/90814373