Based on Java code, numbers appear more than half of the time in the array

Based on Java code, numbers appear more than half of the time in the array

The following uses several methods to introduce the number of occurrences of java array numbers. The specific content is as follows:

method one:

The array is sorted, then the middle value is definitely the one to look for. Minimal time complexity of sorting (quick sort) O(NlogN), plus traversal.

Method Two:

The method of using the hash table is to count the number of occurrences of each array, and output the number with the number of 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 of occurrences of this number is more than the sum of the number of occurrences of other numbers.

Consider deleting two different numbers each time, so among the remaining numbers, the number of occurrences still exceeds the total number, repeat the process continuously, exclude other numbers, and finally find the number that appears more than half of the time. The time complexity of this method is O(N) and the space complexity is O(1).

Another way of thinking, this can be achieved by counting, rather than actually physically deleting. In the process of traversing the 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 number saved before, the number will be increased by 1, and if it is different, the number will be decreased by 1. If the number of times is 0, save the next number and set the number of times to 1. Since the number of times we are looking for appears more than the sum of all other numbers, the number we are looking for must be the last time The corresponding number when set to 1.

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 sort, as mentioned earlier, if an array is sorted, the number in the middle must be the value sought. The time complexity of sorting an array is O(nlog(n)), but for this problem, there are better algorithms that can be found within the time complexity O(n).

Referring to the quick sort algorithm, the Partition() method is the most important method. This method returns an index, which can ensure that the number at the index position has been sorted. The number on the left of the index is smaller than the number at the index. The numbers to the right of the index are greater than the numbers where the index is located. Then this problem can be solved using this idea.

Return index through Partition(). If index==mid, it indicates that the median of the array is found; if indexmid, it indicates that the median is between [start, index-1]. Know that the index==mid cycle ends at the end.

public int Partition(int[] nums,int start,int end){

int pivotkey = nums[start];

int origin = start;

while(start

while(start=pivotkey) end--;

while(start

swap(nums,start,end);

}

swap(nums,start,end);

swap(nums,http://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 the index obtained after adjusting the array is greater than middle, continue to adjust the array from start to index-1

index = Partition(nums, start, index-1);

else{

//Otherwise adjust the array from index+1 to end section

index = Partition(nums, index+1, end);}}

return numshttp://[index];}

The above content introduces the Java code to realize that the numbers appear more than half of the time in the array. I hope it will be helpful to everyone!

while(start=pivotkey) end--;

while(start

swap(nums,start,end);

}

swap(nums,start,end);

swap(nums,http://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 the index obtained after adjusting the array is greater than middle, continue to adjust the array from start to index-1

index = Partition(nums, start, index-1);

else{

//Otherwise adjust the array from index+1 to end section

index = Partition(nums, index+1, end);}}

return numshttp://[index];}

The above content introduces the Java code to realize that the numbers appear more than half of the time in the array. I hope it will be helpful to everyone!

swap(nums,start,end);}

swap(nums,start,end);

swap(nums,http://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 the index obtained after adjusting the array is greater than middle, continue to adjust the array from start to index-1

index = Partition(nums, start, index-1);

else{

//Otherwise adjust the array from index+1 to end section

index = Partition(nums, index+1, end);}}

return numshttp://[index];}

The above content introduces the Java code to realize that the numbers appear more than half of the time in the array. I hope it will be helpful to everyone!

Guess you like

Origin blog.csdn.net/G171104/article/details/131853779