Offer: "39" of the number of occurrences in an array of more than half

Source: Internet
Author: User

Point of Offer: the number of occurrences in an array of more than half "39" title description

If there is a number in the array that appears more than half the length of the array, find this number. For example, enter an array of length 9 {1,2,3,2,2,2,5,4,2}. Since this number 2 appears in the array 5 times, more than half the length of the array, so output 2.

Solution One: An algorithm based on the time complexity of partition function is an O (n) approach

There is a number in the array that appears more than half the length of the array. If this array is sorted, then the number in the middle of the array after sorting must be the number that appears more than half the length of the array. In other words, this number is the median of statistics.

In the random fast algorithm, we now 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 number is on its left and the number larger than the selected number is on its right. If the small mark of the selected number is exactly N/2, then this number is the median of the number, and if its subscript is greater than N/2, then the median should be on its left side, and we can then find it in the array on the left part of it. Otherwise, you can find it in the right!

This is probably what it means:

Java Implementation Code
Import Java.util.scanner;public class Partitiondemo {public static void main (string[] args) {Scanner input = n        EW Scanner (system.in);        String str = input.nextline ();        String[] arrstr =str.split ("");        int[] Arrn = new Int[arrstr.length];        for (int i=0;i<arrstr.length;i++) arrn[i]= integer.parseint (Arrstr[i]);    System.out.println (Morethanhalfnum (arrn,arrn.length));        }//Number more than half of the number public static int morethanhalfnum (int[] numbers,int length) {int middle = length>>1;        int start = 0;        int end = Length-1;        int index = partition (Numbers,start,end);                while (Index!=middle) {if (index>middle) {end = Index-1;            index = partition (Numbers,start,end);                } else{start = index+1;            Index=partition (Numbers,start,end);        }} int result = Numbers[middle];    return result;    }Divide public static int partition (int[] aux,int lo,int hi) {int i=lo,j=hi+1;///left two pointers, respectively, starting from both ends int Val = Aux[lo];            Take the leftmost first element as the pivot element while (true) {while (aux[++i]<val) if (I==hi) break;//left pointer to right to find a greater than pivot while (Aux[--j]>val) if (J==lo) break; Right pointer to left to find an if (i>=j) break greater than the pivot;            This i>=j is very critical, which indicates that it has now been divided successfully, i.e. the elements are ordered on both sides!  Exch (Aux,i,j);        I!=j, said there is still can find! } Exch (Aux,lo,j);        Swap the pivot element to where it should be! Return J;    Put the dividing point back to the area, the purpose is to say, starting from here, the two sides have become the left and right sub-sequences, the return is J this is very critical, because I may be greater than J.        }//Exchange public static void Exch (int[] Aux,int i,int j) {int t=aux[i];        AUX[I]=AUX[J];    aux[j]=t; }}

  

Offer: "39" of the number of occurrences in an array of more than half

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.