Java-based code implementation numbers appear more than half in an array _java

Source: Internet
Author: User

The following are several ways to introduce the number of Java array occurrences, as follows:

Method One:

Array, and then the middle value is definitely the value to look for. Sort the smallest time complexity (quick sort) O (NLOGN), plus traversal.

Method Two:

The way to use a hash table is to count the number of occurrences of each array and the number of output occurrences greater than the length of the array.

Method Three:

The number of occurrences exceeds half the length of the array, indicating that the number appears more often than the number of other occurrences.

Consider deleting two different numbers at a time, and in the remaining number, the number of occurrences still exceeds the total, repeating the process, excluding the other numbers, and finally finding the number that appears more than half. The time complexity of this method is O (N), and the space complexity is O (1).

In other words, this can be done by counting, not really physically deleted. In the process of traversing an array, save two values, one is the number in the array, and the other is the number of occurrences. When traversing to the next number, if the number is the same as the previous saved number, the number is 1, and if it is different, the number is reduced by 1. If the number is 0, save the next number and set the number to 1, because the number we are looking for is more than the sum of all the other numbers, so the number to be found is the last number to set the number to 1 o'clock.

public int morehalf (int[] nums) {
int result = 0;
int count = 1;
if (Nums.length = = 0)
return-1;
result = Nums[0];
for (int i = 1; i < Nums.length. i++) {
if (count = = 0) {result
= Nums[i];
Count = 1;
Continue;
}
if (result = = Nums[i])
count++;
else
count--;
}
return result;
}

Method Four:

Improved quick-row, mentioned earlier, if you sort an array, that number in the middle is definitely the value you're seeking. The time complexity of array ordering is O (Nlog (n)), but there are better algorithms for this problem, which can be obtained in time complexity O (n).

Using the fast sorting algorithm, the partition () method is one of the most important methods, which returns a index to ensure that the number of index positions is sorted and the number on the left of index is smaller than the number of index. The number on the right of index is greater than the number of index. Then this can be used to solve the idea.

The index is returned via partition (), and if index==mid, the median of the array is found, and if indexmid indicates that the median number is between [start,index-1]. Know that the final Index==mid loop ends.

public int Partition (int[] nums,int Start,int end) {
int pivotkey = Nums[start];
int origin = start;
while (Start<end) {while
(Start<end&&nums[end]>=pivotkey) end--;
while (Start<end&&nums[start]<pivotkey) start++;
Swap (nums,start,end);
} 
Swap (nums,start,end);
Swap (nums,origin,end);
return end;
}
Public int[] Swap (int[] ints, int x, int y) {
int temp = ints[x]; 
INTS[X] = Ints[y]; 
Ints[y] = temp; 
return ints; 
} 
public int morethanhalf (int[] nums) {
if (nums.length==0)
return-1;
int start = 0;
int end = Nums.length-1;
int index = Partition (nums, start, end);
int mid = NUMS.LENGTH/2;
while (Index!=mid) {if
(index>mid)
//If Index is greater than middle after the array is adjusted, the array index of the start to Index-1 section continues to be adjusted
Partition (Nums, start, index-1);
else{
//Otherwise adjusts the array 
index = Partition (Nums, index+1, end) of the index+1 to the end section;
}
return nums[index];
}

The above content for you to introduce the Java Code implementation number in the array of more than half of the relevant content, I hope to help you!

Related Article

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.