To say two points to find

Source: Internet
Author: User

Binary search is the algorithm we often encounter, clear thinking, code concise. Binary lookup requires sequential ordering, and support random access, generally we discuss the sequence does not exist the same element, then the binary search can be very skilled to express the following:

int binsearch (int a[], int n, int target)    {        int left=0,right=n-1,res=-1;        while (Left<=right)        {            int mid = left+ ((right-left) >>1);            if (a[mid]<target) left                = mid+1;            else if (a[mid]>target) Right                = mid-1;            else            {                return mid;            }        }        return-1;    }
However, when we do not require the ordered sequence, that is, the same elements may appear, the binary lookup will be back to some of the expansion problems, such as:

    1. Returns the index of the first occurrence of key, if no return-1;
    2. Returns the last occurrence of the key, if no return-1;
    3. Returns the subscript of an element that is just less than key, if no 1 is returned;
    4. Returns the subscript of an element that is just greater than key, if no 1 is returned;
for (1), when the element with the same key is found, it is not returned immediately, but is recorded first, and then continues to look for the same element on the left that has the same key.
int searchlowerbound (int a[], int n, int target)    {        int left=0,right=n-1,res=-1;        while (Left<=right)        {            int mid = left+ ((right-left) >>1);            if (a[mid]<target) left                = mid+1;            else if (a[mid]>target) Right                = mid-1;            else            {                res = mid;                right = Mid-1;            }        }        return res;    }
for (2), the same can be done by:
int searchhigherbound (int a[], int n, int target)    {        int left=0,right=n-1,res=-1;        while (Left<=right)        {            int mid = left+ ((right-left) >>1);            if (a[mid]<target) left                = mid+1;            else if (a[mid]>target) Right                = mid-1;            else            {                res = mid;                left = mid+1;            }        }        return res;    }
for (3), making key=key-1, the conversion is problematic (2), for (4), making key=key+1, then the conversion is problematic (1).
Let me take a look at the two points that appear in a leetcode as an example: search for a rangeTitle Description:

Given a sorted array of integers, find the starting and ending position of a Given target value. Your algorithm ' s runtime complexity must is in the order of O (log n). If the target is not a found in the array, return [-1,-1].

For example,

Given [5, 7, 7, 8, 8, ten] and target value 8,

return [3, 4].

The problem is to ask for the lowest value of the subscript and the largest value subscript, so the code is easy to get:

Class Solution {Public:int searchlowerbound (int a[], int n, int target) {int left=0,right=n-1,res=-1;            while (left<=right) {int mid = left+ ((right-left) >>1);            if (a[mid]<target) left = mid+1;            else if (a[mid]>target) right = mid-1;                else {res = mid;            right = Mid-1;    }} return res;        } int searchhigherbound (int a[], int n, int target) {int left=0,right=n-1,res=-1;            while (left<=right) {int mid = left+ ((right-left) >>1);            if (a[mid]<target) left = mid+1;            else if (a[mid]>target) right = mid-1;                else {res = mid;            left = mid+1;    }} return res; } vector<int> searchrange (int a[], int n, int target) {VECTOR&LT;INT&GT        Res        Res.push_back (Searchlowerbound (a,n,target));        Res.push_back (Searchhigherbound (a,n,target));    return res; }};

To say two points to find

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.