"Sword refers to offer study" "Face question 38: Number of occurrences in sorted array"

Source: Internet
Author: User

Title: Statistics A number: The number of occurrences in the sorted array. examples Show

For example input sort array {1, 2, 3, 3, 3, 3, 4, 5} and number 3, because 3 appears in this array 4 times, so output 4.

Thinking of solving problems

Using an improved binary algorithm.
How to find the first k in the array using the binary search algorithm, the binary search algorithm always compares the numbers in the middle of the array with the K. If the middle number is larger than K, then K can only appear in the first half of the array, and the next round we'll just look in the first half of the array. If the middle number is smaller than k, then K can only appear in the second half of the array, and the next round we'll just look in the second half of the array. What if the median number is equal to K? Let's start by judging if this number is the first K. If the first digit in the middle number is not k, then the middle digit is exactly the number one K. If the first digit of the middle number is also k, that is, the initial k must be in the first half of the array, the next round we still need to find in the first half of the array.
The same idea finds the last K in the sorted array. If the middle number is larger than K, then K can only appear in the first half of the array. If the median number is smaller than K, K can only appear in the second half of the array. What if the median number equals k? We need to determine if this k is the last K, that is, the next number in the middle number is also equal to K. If the next number is not K, then the middle number is the last K: otherwise we will find it in the second half of the array.

Code Implementation
 Public  class Test38 {    /** * Find the position of the first occurrence of k in the sorted array * * @param data * @param k * @param start *
     
       @param End *
       @return *
      /    Private Static int GETFIRSTK(int[] Data,intKintStartintEnd) {if(Data = =NULL|| Data.length <1|| Start > End) {return-1; }intMididx = start + (End-start)/2;intMiddata = Data[mididx];if(Middata = = k) {if(Mididx >0&& Data[mididx-1]! = k | | Mididx = =0) {returnMididx; }Else{end = Mididx-1; }        }Else if(Middata > K) {end = Mididx-1; }Else{start = Mididx +1; }returnGETFIRSTK (data, K, start, end); }/** * Find the last occurrence of K in a sorted array * * @param data * @param k * @param start *< c4> @param End * @return * /    Private Static int GETLASTK(int[] Data,intKintStartintEnd) {if(Data = =NULL|| Data.length <1|| Start > End) {return-1; }intMididx = start + (End-start)/2;intMiddata = Data[mididx];if(Middata = = k) {if(Mididx +1< Data.length && Data[mididx +1]! = k | | Mididx = = Data.length-1) {returnMididx; }Else{start = Mididx +1; }        }Else if(Middata < k) {start = Mididx +1; }Else{end = Mididx-1; }returnGETLASTK (data, K, start, end); }/** * Title: Statistics A number: number of occurrences in the sorted array * @param data * @param k * @return  */     Public Static int GETNUMBEROFK(int[] Data,intK) {intNumber =0;if(Data! =NULL&& data.length >0) {intFirst = GETFIRSTK (data, K,0, Data.length-1);intLast = Getlastk (data, K,0, Data.length-1);if(First >-1&& Last >-1) {Number = Last-first +1; }        }returnNumber } Public Static void Main(string[] args) {//Find the number in the middle of the array now        int[] Data1 = {1,2,3,3,3,3,4,5}; System.out.println (GETNUMBEROFK (Data1,3));//4        //Lookup array appears at the beginning of the array        int[] Data2 = {3,3,3,3,4,5}; System.out.println (GETNUMBEROFK (data2,3));//4        //Lookup array appears at the end of the array        int[] Data3 = {1,2,3,3,3,3}; System.out.println (GETNUMBEROFK (data3,3));//4        //The number found does not exist        int[] data4 = {1,3,3,3,3,4,5}; System.out.println (GETNUMBEROFK (DATA4,2));//0        //The number found is smaller than the first number and does not exist        int[] Data5 = {1,3,3,3,3,4,5}; System.out.println (GETNUMBEROFK (DATA5,0));//0        //Find a number that is larger than the last number and does not exist        int[] Data6 = {1,3,3,3,3,4,5}; System.out.println (GETNUMBEROFK (DATA6,0));//0        the numbers in the//array are the numbers that are searched from beginning to end        int[] Data7 = {3,3,3,3}; System.out.println (GETNUMBEROFK (Data7,3));//4        the number in the array is only a repeating number from beginning to end, not the number found        int[] Data8 = {3,3,3,3}; System.out.println (GETNUMBEROFK (Data8,4));//0        //array with only one number, is the number to find        int[] data9 = {3}; System.out.println (GETNUMBEROFK (DATA9,3));//1        //array with only one number, not a lookup number        int[] data10 = {3}; System.out.println (GETNUMBEROFK (DATA10,4));//0}}
Run Results

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Sword refers to offer study" "Face question 38: Number of occurrences in 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.