Number of occurrences more than half in an array

Source: Internet
Author: User



"Sword Point offer"P163

Title: Find a number in the array that appears more often than the entire length of the array

Solution One: The original problem is converted to the median of the array, using the idea of fast ordering, each time the Partition take the Last Sentinel, the traversal will be less than, greater than the number of Sentinels respectively to the Sentinel, and finally return to the Sentinel in the processed array position. Shrinking the length of the array to be processed, finally determining the element that returns a value that is half the length of the number, which is the median.

Solution Two: Because the number of occurrences of the problem is greater than the number of all other numbers appear, it is used two variables, one represents the number of Num_data, a number of times; when the next number equals Num_data Add 1; if not equal, time minus 1, until times equals 0, replace num_data with the next number; The result of the final Num_data must be the desired value.

public class Morethanhalfnum {/***************** implementation method one **********************///solution one: Based on the partition method public int MORETHANHALFNUM1 (int[] data) {if (data = = null) | | data.length = = 0) {return 0;} First Partitionint target = data.length >> 1;//target indexint start = 0;int end = data.length-1;//Set default initial value int index = Parttition (data, start, end), while (index! = target) {if (Index < target)//indicates median in Data[index] the second half {start = index + 1;} else//indicates median in Data[index] the first half of {end = Index-1;} index = parttition (data, start, end);} Determine if the number found in the original array does have a &GT;N/2 return Judge (data, Data[index]);} Determine if the number found in the original array is indeed &GT;N/2 a private int Judge (int[] data, int target_num) {int times = 0;for (int num:data) {if (num = = target_ num) {times++;}} return times*2 > Data.length? target_num:0;} Quick Sort Core Method private int parttition (int[] data,int Start,int end) {//select Sentry int mid_num = Data[end];int index = start;//location record for (int i = start; i < end; i++) {if (Data[i] > Mid_num) {Swap (data, I, index);} else {index++;}} Swap (data, index, end); ReTurn index;} Cannot use reference implementations like C + +, so it has to be changed with the data array private void Swap (int[] data, int a, int b) {int temp = Data[a];d ata[a] = data[b];d ata[b] = temp;} /***************** Implementation method two ******************/public int MoreThanHalfNum2 (int[] data) {if (data = = null) | | data.length = 0 ) {return 0;} int num = data[0];//record data int times = 1;//Record count for (int data_num:data) {if (0 = = times) {num = data_num;times + +;} else if (num = = data_num) {times++;} else{times--;}} return Judge (data, num);} public static void Main (string[] args) {int[] data = {1,2,3,2,2,2,5,4,2}; Morethanhalfnum morethanhalfnum = new Morethanhalfnum (); System.out.println (morethanhalfnum.morethanhalfnum2 (data));}}

Number of occurrences more than half in an 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.