Title: There is a number in the array that appears more than half the length of the array, please find this number.
For example, enter an array of length 9 {1,2,3,2. 2, 2. 5,4,2}. Because the number 2 appears 5 times in the array, it exceeds half the length of the array, so the output is 2.
Solution One: an O (n) algorithm based on the partition function:
Our algorithm is inspired by the high-speed sequencing algorithm. In the algorithm of random high-speed sorting. Let's start by randomly selecting a number in the array. The order of the numbers in the array is then adjusted so that the number smaller than the selected digit is on its left. A number larger than the selected number is on its right. For example, the subscript of the selected number is just N/2. Then the number is the median in the array. Suppose its subscript is greater than N/2. Then the median should be on its left. We can then find it in the array on the left part of it. Assuming its subscript is less than N/2, the median should be on its right, and we can then find it in the right part of it. This is a typical recursive process.
Java code Implementation steps such as the following:
/** * */package swordforoffer;/** * @author Jinshuangqi * * August 8, 2015 */public class E29morethanhalfnumber {//applicable partition function public int partition (int[] Arr,int left,int right) {int result = Arr[left];if (Left > right) return-1;while (left <ri Ght) {while (left <right && arr[right]>= result) {right--;} Arr[left] = Arr[right];while (left <right && Arr[left] <result) {left++;} Arr[right] = Arr[left];} Arr[left] = Result;return left;} public int morethanhalfnum (int[] arr) {if (arr.length ==0) return-1;int length = arr.length;int middle = length >>1;in t start = 0;int end = Length-1;int index = partition (Arr,start,end), while (Index! = middle) {if (index >middle) {end = Ind Ex-1;index = partition (Arr,start,end);} Else{start = index + 1;index = partition (Arr,start,end);}} int result = Arr[middle];if (!checkmorethanhalf (Arr,result)) {result =-1;} return result;} Verify that there is a public boolean checkmorethanhalf (int[] arr,int number) {int times = 0;for (int i = 0;i<arr.length;i++) {iF (arr[i] = = number) times + +;} Boolean ismorethanhalf = True;if (Times <= arr.length) {ismorethanhalf = false;} return ismorethanhalf;} public static void Main (string[] args) {int[] arr= {1,2,3,3,2,5,4,2}; E29morethanhalfnumber test = new E29morethanhalfnumber (); System.out.println (Test.morethanhalfnum (arr));}}
solution Two: According to the characteristics of the array to find O (n) algorithm:
Next we will solve the problem from another angle.
There is a number in the array that occurs more than half the length of the array. This means that it appears more often than the number of all other numbers. So we can iterate over the array and save two values: One is a number in the array, and the other is the number of times. When we traverse to the next number. Assuming that the next number is the same as the number we saved earlier, the number of times is 1, assuming that the next number is different from the number we saved earlier, the number of times minus 1. If the number of times is 0, we need to save the next number and set the number of times to 1. Because the number we're looking for is more than the number of So the number to look for is definitely the last time to set the number to 1 o'clock corresponding number.
Solution two: public int moreThanHalfNum2 (int[] arr) {if (arr.length = = 0) return-1;int result = arr[0];int times = 1;for (int i = 1; i<arr.length;i++) {if (times = = 0) {result = Arr[i];times = 1;} else if (arr[i] = = result) times++;elsetimes--;} if (!checkmorethanhalf (arr,result)) result = -1;return result;}
Sword Point of Offer (Java Edition): Number of occurrences more than half in an array