Question 38: Number of occurrences of a number in a sorted array

Source: Internet
Author: User

Topic:

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

Ideas:

1. Sequential traversal

Sequentially scans the array once, counting the number of occurrences of that figure.

Time complexity: O (N)

2, two-point search

Assuming that the number we are looking for is k, then we need to find the first k in the array and the position where the last K appears.

How can I find the position of the first k by two points?

The numbers in the middle of the array are compared with K,

If the number is larger than K, then K can only appear in the first half, then the next round can only be found in the first part;

If the number is smaller than k, then K can only appear in the second half, then the next round can only be found in the latter part;

If the number is equal to K, you need to determine if this is not the first k, if the number of the previous number is not K, then the number is the first k, otherwise the first part of the need to continue to find a k;

The method of finding the last K is the same as finding the first K.

Code:
#include <iostream>using namespace Std;int getfirstk (int* data,int k,int Start,int end) {while (start<=end) {        int mid=start+ ((end-start) >>1);            if (data[mid]==k) {if ((mid>0 && data[mid-1]!=k) | | mid==0) return mid;        else end=mid-1;        } else if (data[mid]>k) end=mid-1;    else start=mid+1; } return-1;} int Getlastk (int* data,int length,int k,int Start,int end) {while (start<=end) {int mid=start+ (End-start) >        &GT;1);            if (data[mid]==k) {if ((mid<length-1 && data[mid+1]!=k) | | mid==length-1) return mid;        else start=mid+1;        } else if (data[mid]<k) start=mid+1;    else end=mid-1; } return-1;}    int GETNUMBEROFK (int* data,int length,int k) {if (Data==null | | length<=0) return 0;    int First=getfirstk (DATA,K,0,LENGTH-1);int Last=getlastk (DATA,LENGTH,K,0,LENGTH-1);    cout<<first<< "" <<last<<endl;    if (first!=-1 && last!=-1) return last-first+1; return 0;}    int main () {int a[]={1,2,3,3,3,3,4,5};    int len=sizeof (A)/sizeof (a[0]);    int k=3;    cout << GETNUMBEROFK (a,len,k) << Endl; return 0;}
Online test OJ:

http://www.nowcoder.com/books/coding-interviews/70610bf967994b22bb1c26f9ae901fa2?rp=2

AC Code:

Class Solution {Public:int GETNUMBEROFK (vector<int> data,int k) {int len=data.size ();        if (len<=0) return 0;        int First=getfirstk (data,k,0,len-1);        int Last=getlastk (data,len,k,0,len-1);        if (first!=-1 && last!=-1) return last-first+1;    return 0;        } int Getfirstk (const vector<int> &data,int k,int Start,int end) {int mid;            while (start<=end) {mid=start+ ((End-start) >>1);                if (data[mid]==k) {if ((mid>0 && data[mid-1]!=k) | | mid==0) return mid;            else end=mid-1;            } else if (data[mid]>k) end=mid-1;        else start=mid+1;    } return-1;        } int Getlastk (const vector<int> &data,int length,int k,int start,int end) {int mid; while (start<=end) {mid=start+ ((End-start) >>1);            if (data[mid]==k) {if (mid<length-1 && data[mid+1]!=k) | | | mid==length-1)                return mid;            else start=mid+1;            } else if (data[mid]>k) end=mid-1;        else start=mid+1;    } return-1; }};

(point of Offer) Question 38: Number of occurrences in the sorted array

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.