The offer--of the Sword 38: Number of occurrences of the number in the sorted array (using two-point lookup to find the first and last position)

Source: Internet
Author: User

Topic:

Counts the number of occurrences of a number in a sorted array.

Ideas:

Because it is a well-ordered array, a binary lookup algorithm can be used.

Generally the most easily thought of the idea is to use a binary search to find a first, and then go to his left and right side of the traverse, but this method because in the N array may also have n K, so

The complexity of the lookup is still O (n)

You can first find the first occurrence by using the binary lookup algorithm, that is, when you find one, see if it is either k or whether it is the first one to find this paragraph.

Then the same two-point search to find the last occurrence of the position.

1#include <iostream>2#include <vector>3 using namespacestd;4 5 intCount_number (vector<int>& VEC,intN)6 {7     if(Vec.empty ())8         return 0;9     intlen=vec.size ();Ten     intRe=0; One     intI=0; A     intj=len-1; -  -     intre1=0; the      while(1) -     { -         if(i==j) -         { +             if(vec[i]==N) -{re1=i; Break;} +             Else A                 return 0; at         } -         intk= (j+i)/2; -  -         if(vec[k]==N) -         { -             if(k==i) in{re1=k; Break;} -             if(vec[k-1]!=N) to{re1=k; Break;} +j=k-1; -         } the         Else if(vec[k]<N) *         { $i=k+1;Panax Notoginseng         } -         Else thej=k-1; +     } A  thecout<<"re1="<<re1<<Endl; +  -     intRe2=0; $I=0; $j=len-1; -      while(1) -     { the         if(i==j) -         {Wuyi             if(vec[i]==N) the{re2=i; Break;} -             Else Wu                 return 0; -         } About         intk= (j+i)/2; $  -         if(vec[k]==N) -         { -             if(k==j) A{re2=k; Break;} +             if(vec[k+1]!=N) the{re2=k; Break;} -i=k+1; $         } the         Else if(vec[k]<N) the         { thei=k+1; the         } -         Else inj=k-1; the     } thecout<<"re2="<<re2<<Endl; Aboutre=re2-re1+1; the     returnre; the } the  + intMain () - { the     intary[8]={1,2,3,3,3,3,4,5};Bayivector<int> Vec (ary,ary+8); the  theCout<<count_number (VEC,3) <<Endl; -System"Pause"); -}

The offer--of the Sword 38: Number of occurrences of the number in the sorted array (using two-point lookup to find the first and last position)

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.