More than half the number in the array 2.3

Source: Internet
Author: User

Two methods, one is based on the partition function of the fast row, but this exists a problem if the array {1,1,1,1,1,1,2,3,4,5,6}, so that thepartition returns the number as 2 The corresponding index

??

So this method needs to add a number that determines whether there are more than half of the digits in the array

??

The other is, first think the first number is what we want to find, set a time, initially 1, and then traverse the following number, if the same as this number, then time++, different time--, when time is reduced to 0 , the number is replaced with the current number , and it is set to 1and then traversed

??

You should also determine if the input array meets the requirements

??

Package moreThanHalfNum29;

??

public class MoreThanHalfNum29 {

static int Morethanhalfnum (int[] A, int length) {

if (length = = 0) {

return 0;

}

int mid = length >> 1;

int start = 0;

int end = Length-1;

int index = partition (A, start, end);

while (index! = mid) {

if (Index > mid) {

end = Index-1;

index = partition (A, start, end);

} else {

Start = index + 1;

index = partition (A, start, end);

}

}

int result = A[mid];

return result;

??

}

??

static int partition (int[] A, int left, int. right) {

int I, J, T, Key;

if (left >= right)

return 0;

??

key = A[left];

i = left;

j = right;

while (i! = j) {

while (A[j] >= key && i < J)

j--;

while (A[i] <= key && i < J)

i++;

if (I < j) {

t = A[i];

A[i] = A[j];

A[J] = t;

}

}

A[left] = A[i];

A[i] = key;

return i;

}

??

static int sol2 (int[] a) {

if (Checkinvalidarray (a)) {

return 0;

}

int result=a[0];

int Times=1;

for (int i = 0; i < a.length; i++) {

if (times==0) {

Result=a[i];

Times=1;

}else {

if (Result==a[i]) {

times++;

}else {

times--;

}

}

}

return result;

??

}

??

private static Boolean Checkinvalidarray (Int[] a) {

if (a = = NULL | | a.length = = 0) {

return true;

} else {

return false;

}

}

??

public static void Main (string[] args) {

TODO auto-generated Method Stub

Int[] A = {3, 1, 2, 2, 2, 2, 2, 5, 6};

SYSTEM.OUT.PRINTLN (partition (A, 0, a.length-1));

//???????????????? int result = Morethanhalfnum (A, 9);

int Result=sol2 (a);

SYSTEM.OUT.PRINTLN (result);

//???????????????? for (int i:a)

//???????????????????????? System.out.println (i);

}

??

}

More than half the number in the array 2.3

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.