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}. Since the number 2 appears 5 times in the array, it exceeds half the length of the array, so output 2.
Solution One: an O (n) algorithm based on the partition function:
Our algorithm is inspired by the fast-sorting algorithm. In the algorithm of random fast sorting, we first randomly select a number in the array and then adjust the order of the numbers in the array so that the number smaller than the selected digit is on its left and the number larger than the selected number is on its right. For example, if the subscript of the selected number is just N/2, then this number is the median in the array. If its subscript is greater than N/2, then the median should be on the left side of it, and we can then find it in the array on the left part of it. If its subscript is less than N/2, then 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.
The Java code implementation process is as follows:
/** * */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 this problem from another angle. There is a number in the array that occurs more than half the length of the array, which means that it appears more often than any other number. 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, the number is 1 if the next number is the same as the number we saved earlier, and if the next number is different from the one we saved earlier, The number of times is reduced by 1. If the number is 0, we need to save the next number and set the number of times to 1. Since the number we are looking for is more than the sum of the number of other numbers appearing, the number to be searched is definitely the last number to be set at 1 o'clock.
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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sword Point of Offer (Java Edition): Number of occurrences more than half in an array