The sword refers to the offer series---number of occurrences in the array

Source: Internet
Author: User

"title" counts the number of occurrences of a number in a sorted array.

1  PackageCom.exe9.offer;2 3 /**4 * "title" counts the number of occurrences of a number in a sorted array. 5  * @authorWGS6  *7  */8  Public classGETNUMOFK {9      Public intGETNUMOFK (int[] arr,inttarget) {Ten         if(arr==NULL|| arr.length<=0)return-1; One         intlen=arr.length; A         intCount=0; -          -          the         intLASTINDEX=FINDLASTK (arr, target, 0, len-1); -System.out.println ("= = =" +lastIndex); -         if(Lastindex==-1) -             return-1; +         intFIRSTINDEX=FINDFIRSTK (arr, target, 0, len-1); -System.out.println ("= = =" +firstindex); +          A         if(Firstindex>=0 && lastindex>=0) atCount=lastindex-firstindex+1; -          -         returncount; -          -     } -      in     //first, look for the duplicate numbers out of the left position. -      Public intFINDFIRSTK (int[] arr,intTargetintStartintend) {     to         if(Start>end)return-1; +          -         intmidindex= (start+end)/2; the         intMidval=Arr[midindex]; *          $         if(midval==target) {Panax Notoginseng              while(midval==target) { -midindex--; theMidval=Arr[midindex]; +             } A             returnMidindex+1;  the}Else if(midval<target) { +Start=midindex+1; -}Else{//Midval>target $End=midindex-1; $         } -          -         returnFindfirstk (arr,target,start,end); the          -     }Wuyi     ////first, look for the duplicate numbers out of the right position. the      Public intFINDLASTK (int[] arr,intTargetintStartintend) { -         if(Start>end)return-1; Wu         intmidindex= (start+end)/2; -         intMidval=Arr[midindex]; About          $         if(midval==target) { -              while(midval==target) { -midindex++;  -Midval=Arr[midindex]; A             } +             returnMidIndex-1;  the}Else if(midval<target) { -Start=midindex+1; $}Else{//Midval>target theEnd=midindex-1; the         } the          the         returnFindfirstk (arr,target,start,end);  -          in     } the  the      Public Static voidMain (string[] args) { About         int[] arr=New int[]{1}; theGETNUMOFK g=NewGETNUMOFK (); the         intNUM=G.GETNUMOFK (arr, 3); the System.out.println (num); +  -     } the Bayi}

The sword refers to the offer series---number of occurrences in the 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.