Topic:
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 the number 2 appears in the array 5 times, which exceeds half the length of the array, the output is 2.
Ideas:
1. Method 1:
Sort first, then find the median;
Time complexity O (NLOGN)
2. Method 2:
O (n) algorithm based on partition function, that is, to find a large number of K (K=N/2);
Average time complexity: O (N)
3. Method 3:
According to the characteristics of the array, Moore-vote method is adopted.
Since this number appears more often than all other numbers, consider saving two values when iterating through the array: one is a number in an array, and the other is the number of times. When traversing to the next number, if the next number is the same as the previous saved number, the number of times is 1, if different, the number of times minus 1, if the number is 0, you need to save the next number, and set the number of times to 1. Since the number we are looking for appears to be greater than the number of occurrences of all other numbers, the number to be searched must be the number of times the group is set to 1 o'clock.
The time complexity is O (n) and the spatial complexity is O (1).
Code:
Method 2:
#include <iostream>using namespace Std;bool g_binputinvalid=false;bool checkinvalidarray (int* numbers,int Length) {if (Numbers==null | | length<=0) g_binputinvalid=true; return g_binputinvalid;} BOOL Checkmorethanhalf (int* numbers,int length,int number) {int times=0; for (int i=0;i<length;i++) {if (numbers[i]==number) times++; } bool Ismorethanhalf=true; if (times*2<=length) {g_binputinvalid=true; Ismorethanhalf=false; } return ismorethanhalf;} int Partition (int* numbers,int start,int end) {int Key=numbers[start]; int I=start; int j=end; while (I<J) {while (i<j && numbers[j]>=key)--j; if (i<j) numbers[i++]=numbers[j]; while (i<j && numbers[i]<=key) ++i; if (i<j) numbers[j--]=numbers[i]; } Numbers[i]=key; return i;} int Morethanhalfnum (int* numbers,int length) {if (Checkinvalidarray (numbers,length)) RetuRN 0; int mid=length>>1; int start=0; int end=length-1; int index=partition (numbers,start,end); while (Index!=mid) {if (index>mid) {end=index-1; Index=partition (Numbers,start,end); } else{start=index+1; Index=partition (Numbers,start,end); }} int Result=numbers[index]; if (! Checkmorethanhalf (Numbers,length,result)) result=0; return result;} int main () {int a[]={3,1,3,2,4,3,3,5,3,7,3}; int length=sizeof (A)/sizeof (a[0]); cout << morethanhalfnum (a,length) << Endl; return 0;}
Method 3:
#include <iostream>using namespace Std;bool g_binputinvalid=false;bool checkinvalidarray (int* numbers,int Length) {if (Numbers==null | | length<=0) g_binputinvalid=true; return g_binputinvalid;} BOOL Checkmorethanhalf (int* numbers,int length,int number) {int times=0; for (int i=0;i<length;i++) {if (numbers[i]==number) times++; } bool Ismorethanhalf=true; if (times*2<=length) {g_binputinvalid=true; Ismorethanhalf=false; } return ismorethanhalf;} int morethanhalfnum_1 (int* numbers,int length) {if (Checkinvalidarray (numbers,length)) return 0; int result=numbers[0]; int Times=1; for (int i=1;i<length;i++) {if (times==0) {result=numbers[i]; Times=1; } if (Result==numbers[i]) times++; else times--; } if (! Checkmorethanhalf (Numbers,length,result)) result=0; return result;} int main () {int a[]={3,1,3,2,4,3,3,5,3,7,3}; int length=sizeof (A)/sizeof (a[0]); cout << morethanhalfnum_1 (a,length) << Endl; return 0;}Online test OJ:
http://www.nowcoder.com/books/coding-interviews/e8a1b01a2df14cb2b228b30ee6a92163?rp=2
AC Code:
Method 1:
Class Solution {Public:int Partition (vector<int> &numbers,int start,int end) {int Key=numbers[start]; int I=start; int j=end; while (I<J) {while (i<j && numbers[j]>=key)--j; if (i<j) numbers[i++]=numbers[j]; while (i<j && numbers[i]<=key) ++i; if (i<j) numbers[j--]=numbers[i]; } Numbers[i]=key; return i; } int morethanhalfnum_solution (vector<int> numbers) {int length=numbers.size (); if (length<=0) return 0; int mid=length>>1; int start=0; int end=length-1; int index=partition (numbers,start,end); while (Index!=mid) {if (index>mid) {end=index-1; Index=partition (Numbers,start,end); } else{start=index+1; Index=partition (Numbers,start,end); }} int Result=numbers[index]; int times=0; for (int i=0;i<length;i++) {if (numbers[i]==result) times++; } if (times*2<=length) result=0; return result; }};
Method 2:
Class Solution {public: int morethanhalfnum_solution (vector<int> numbers) { int length=numbers.size (); if (length<=0) return 0; int result=numbers[0]; int Times=1; for (int i=1;i<length;i++) { if (times==0) { result=numbers[i]; Times=1; } if (Result==numbers[i]) times++; else times--; } int t=0; for (int i=0;i<length;i++) { if (numbers[i]==result) t++; } if (t*2<=length) result=0; return result;} ;
Question 29: Number of occurrences of more than half the number of occurrences in an array