Sword-to-be-offer problem solving report (Java Edition)--number of 38 in sorted array

Source: Internet
Author: User

??

Analyze problems

??

The problem only needs to find the number of the number of K in the sorted array, because it is already sorted,K must be in a heap, so we just need to find the first k index1, and then find the last K the index2 will be ready.

??

And the process of finding K We generally find by dichotomy, so that time complexity can be reduced to Logn

??

Solve the problem

??

We find K by dichotomy, if the middle number is less than K, then in the first half to find K; If the middle number is greater than K, then in the second half to find K, then how to determine the K found is it a bunch of K 's boundaries?

??

To find the first K when the way to determine the following:

If the middle number equals K, then the first number is determined to exist, if it exists and is equal to K, in the first half of the

If the existence is not equal to K or does not exist, then this number is the first K

??

When you find the last K , the way to judge it is as follows:

If the middle number equals K, then first determine whether the number exists, if present and equal to K, in the second half of the search, if there is not equal to K or does not exist, then this number is the last K

??

Main code

??

int Getlastk (int[] data,int length,int k,int start,int end) {

??

if (start>end) {

return-1;

}

??

int midindex= (start+end)/2;

int Middata=data[midindex];

if (data[midindex]==k) {

if ((midindex<length-1&&data[midindex+1]!=k) | | | MIDINDEX==LENGTH-1) {

return midindex;

}else {

start=midindex+1;

}

}else if (middata<k) {

start=midindex+1;

}else {

End=midindex-1;

}

Return Getlastk (Data,length,k,start,end);

??

}

??

int Getfirstk (int[] data,int length,int k,int start,int end) {

if (start>end) {

return-1;

}

??

int midindex= (start+end)/2;

int Middata=data[midindex];

if (data[midindex]==k) {

if ((midindex>0&&data[midindex-1]!=k) | | | midindex==0) {

return midindex;

}else {

End=midindex-1;

}

}else if (middata>k) {

End=midindex-1;

}else {

start=midindex+1;

}

Return Getfirstk (Data,length,k,start,end);

??

}

??

int GETNUMOFK (int[] Data,int length,int k) {

int result = 0;

if (data!=null&&length>0) {

int FIRST=GETFIRSTK (data, length, K, 0, length-1);

int LAST=GETLASTK (data, length, K, 0, length-1);

SYSTEM.OUT.PRINTLN (first);

System.out.println (last);

if (first>-1&&last>-1) {

result=last-first+1;

}

}

return result;

}

Sword-to-be-offer problem solving report (Java Edition)--number of 38 in sorted array

Related Article

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.