Translated from:http://blog.csdn.net/zhq651/article/details/7930284
Method 1: since more than half, then use this number and other numbers paired, the remaining number must be more than half of this number. So this number is the number you are looking for by constantly deleting the different 2 numbers until there are no different 2 numbers. Proof: In the worst case, only this number is paired with the other number, and is deleted, and the remainder is still the number, so it is proved.
transferred from:http://www.cnblogs.com/python27/archive/2011/12/15/2289534.html Example: Array a[5]={0,1,2,1,1};
We are looking for a number of 1, the procedure is: traverse the entire array, and then delete the different two numbers at a time, the process is as follows:
0 1 2 1 1 =>2 1 1=>1
Implementation: When we consider deleting two different numbers, we can actually do it with the same count instead of physically deleting it. save two values when iterating over an array: One is a number in an array, and the other is the number of times. When we traverse to the next number, the number is 1If the next number is the same as the number we saved earlier. If the next number differs from the number we saved earlier, the number is reduced by 1. If the number is zero, we need to save the next number and set the number to 1. Since the number we are looking for appears to be more than the sum of the number of other numbers appearing, the number to be searched is definitely the number that corresponds to the last time the number is set to 1 .
Based on this idea, it is not difficult to write the following code:
#include <iostream>#include<string>using namespacestd;//Global variables to check if the input is validBOOLInvalidinput =false;/************************************************************/* Find the number of occurrences in the array that are more than half the length of the array/************************ ************************************/intNumberappearmorethanhalf (int* numbers,unsignedintlength) { if(numbers = = NULL | | length <=0) {Invalidinput=true; return 0; } invalidinput=false; intKey = numbers[0]; unsignedintAppeartimes =1; for(inti =1; I < length;++i) {if(Appeartimes = =0) {Key=Numbers[i]; Appeartimes=1; } if(Numbers[i] = =key) Appeartimes++; ElseAppeartimes--; } //Verify that the input array is a number that satisfies the criteriaAppeartimes =0; for(i =0; i < length; i++) { if(Numbers[i] = =key) Appeartimes++; } if(Appeartimes <= Length/2) {Invalidinput=true; return 0; } returnkey;}intMain () {cout<<"Enter The length of your array:"<<Endl; intArraylength =0; CIN>>arraylength; cout<<"Enter The elements of your array:"<<Endl; int*array =New int[Arraylength]; for(intK =0; K < arraylength;k++) {cin>>Array[k]; } cout<<"The number appear more than half length of your array is:"<<Endl; cout<<numberappearmorethanhalf (array,arraylength) <<Endl; Delete[] array; return 0;}
Recently in the study of Python, by the way, with Python implementation, the programming is rough, hope understanding!
1 defn_on (L):2 PrintL3I,j =0,04 whileLen (L) > 2:5 ifL[i]! = l[j+1]:6 delL[i]7 delL[j]8I,j =0,09 Else:Tenj = j + 1 One PrintL[0] A -L = [2,1,3,1,4,1,1] - n_on (L) the - #[2, 1, 3, 1, 4, 1, 1] - #1 - #[finished in 0.2s]
Method 2: It is a voting idea, 2 variables are defined, 1 are candidates, and the other 1 are voting numbers. If the next person votes the same, the number of votes increases by 1, otherwise minus 1, and when the number of votes is less than 0 o'clock, the candidate is replaced at this time. Once again, the last thing left is the number. Proof: 1 of them voted more than half, then the remainder of the ticket cycle ends, must be less than 0, through the anyway.
Transferred from:http://kenby.iteye.com/blog/1031114
Algorithm: Make full use of more than half of the occurrences, using two variables candidate and vote, representing candidates and votes, respectively, to iterate over the array.
Vote and replace candidates as follows:
1) If the current number is the same as the candidate, add 1 to the candidate's vote
2) If the current number is not the same as the candidate, the number of votes is reduced by 1, if the number of votes is less than 0, then the candidate is kicked off, with the current number as a new candidate
3) The last remaining candidate is more than half the number of occurrences.
The correctness of the algorithm proves that: in the array, the numbers of the same number will vote in favor of the votes, the value of the different will vote against, there is a number of more than half, the other number will be more than half of the negative, so the other number, any one of the votes would be less than 0, were eliminated. The only thing left is more than half the number.
#include <stdio.h>intMainintargcChar**argv) { intI, candidate, vote; inta[Ten]={1,2,3,1,2,1,1,6,1,1}; Candidate=1<< to; Vote=0; for(i =0; I <Ten; i++) { if(A[i]! =candidate) { if(vote = =0) {/*scrap candidate, put A[i] as a new candidate*/candidate=A[i]; Vote=1; } Else{/*candidate's vote minus 1.*/vote--; } } Else{/*candidate's ticket plus 1*/vote++; } } //The last remaining candidate is the number of occurrences more than halfprintf"candidate =%d, vote =%d\n", candidate, vote); return 0;}
Also give the Python code:
1A = [1,2,3,1,2,1,1,6,1,1]2Can,vote = 1, 03 forIinchxrange (0,len (a)):4 ifA[i]! =can:5 ifVote = =0:6can =A[i]7Vote = 18 Else:9Vote = Vote-1Ten Else: OneVote = vote + 1 A PrintCan
"Go" is known to be more than half the number of occurrences, please use the O (n) complexity algorithm to find this number