Title Description
Count the number of occurrences of a number in a sorted array
Ideas
Idea one: violence, simple and rude, but not desirable
Idea two: Because the problem is the sort of array, so we have to think of binary search , so we first use a binary search to find out where a k appears, and then go forward and backward to find the total number.
Idea three: or the idea of two-point search, first find the first k and the last K position subtraction
Code implementation
Package Array;/*** Number of occurrences in a sorted array* Counts the number of occurrences of a number in a sorted array. */ Public classSolution33 { Public Static void Main(string[] args) {Solution33 Solution33 =New Solution33();int[] Array = {1,3,4,5,5,6,7,8,9,Ten}; System. out.println(Solution33.Getnumberofk_3(Array,5)); }/*** Binary search find the first k and the last K subtraction of the two positions * * @param array * @param k* @return */ Public int Getnumberofk_3(int[] Array,intK) {if(Array = =NULL|| Array.length==0)return 0;intNumber =0;intFirst =Getfirstindex(Array, K,0, array.length-1);intLast =Getlastindex(Array, K,0, array.length-1);if(First >-1&& Last >-1) Number = Last-first +1;returnNumber }/*** Find the location of the last K * * @param array * @param k * @param Left * @param Right* @return */ Private int Getlastindex(int[] Array,intKintLeftintright) {if(Left > right)return-1;intMid = left + ((right-left) >>1);if(Array[mid] = = k) {if(Mid = = Right | | (Array[mid +1] = k)) {returnMid }Else{left = mid +1; } }Else if(Array[mid] > K) {right = mid-1; }Else{left = mid +1; }return Getlastindex(array, k, left, right); }/*** Find the location of the first K * * @param array * @param k * @param Left * @param Right* @return */ Private int Getfirstindex(int[] Array,intKintLeftintright) {if(Left > right)return-1;intMid = left + ((right-left) >>1);if(Array[mid] = = k) {if(Mid = = Left | | (Array[mid-1] = k)) {returnMid }Else{right = mid-1; } }Else if(Array[mid] > K) {right = mid-1; }Else{left = mid +1; }return Getfirstindex(array, k, left, right); }/*** Find out where a k appears by using a binary search, and then find the total number of points forward and backward, respectively. * * @param array * @param k* @return */ Public int GETNUMBEROFK(int[] Array,intK) {if(Array.length==0|| Array = =NULL)return 0;intleft =0;intright = array.length-1;intCount =0; while(left <= right) {intMid = left + (right-left)/2;//If the array is too large to prevent overflow if(Array[mid] = = k) {count++;//Determine if there are elements equal to K in front if(Mid > left) {if(Array[mid-1] = = k) { for(inti = mid-1; I >=0; i--) {if(Array[i] = = k) {count++; }Else Break; } } }//Determine if there are elements equal to K in the back if(Mid < right) {if(Array[mid +1] = = k) { for(inti = mid +1; I <= right; i++) {if(Array[i] = = k) {count++; }Else Break; } } }returnCount }Else if(Array[mid] < K) {left = mid +1; }Else{right = mid-1; } }return 0; }/*** Violence * * @param array * @param k* @return */ Public int getnumberofk_2(int[] Array,intK) {if(Array.length==0|| Array = =NULL)return 0;intCount =0; for(inti =0; I < array.length; i++) {if(Array[i] = = k) {count++; } }returnCount }}
The number of times a offer-number appears in a sorted array