Topic source "Sword refers to offer" face question 29, "The beauty of programming" 2.3
Title: There is a number in the array that appears more than half the length of the array, please 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.
Solution 1: There is a number in the array that appears more than half the number of occurrences, which means that it appears more often than any other number. So we can consider preserving 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 reduced by 1 if the next number differs from the number we saved. 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 all the other numbers, the number we're looking for is definitely the last number to be set at 1 o'clock.
BOOLG_invalid_input =false;BOOLCheckinvalidarray (int* Numbers,intLen) {G_invalid_input=Fasle; if(numbers = = NULL | | Len <=0) G_invalid_input=true; returnG_invalid_input;}BOOLCheckmorethanhalf (int* Numbers,intLenintcandidate) { intTimes =0; for(inti =0; i < Len; i++) if(Numbers[i] = =candidate) times++; if(Times *2<Len) {G_invalid_input=true; returnFasle; } return true;}intMorethanhalf (int* Numbers,intLen) { if(Checkinvalidarray (numers, Len))return 0; intcandidate = numbers[0]; intTimes =1; for(inti =1; i < Len; i++) { if(Times = =0) {candidate=Numbers[i]; Times=1; } Else { if(Candidate = =numbers[i]) times++; Else Times--; } } if(!morethanhalf (Numbers, Len, candidate))return 0; returncandidate;}
Solution Two: There is a number in the array that appears more than half the length of the array. If this array is sorted, then the number in the middle of the array after sorting must be the number that appears more than half the length.
BOOLG_invalid_input =false;BOOLCheckinvalidarray (int* Numbers,intLen) {G_invalid_input=Fasle; if(numbers = = NULL | | Len <=0) G_invalid_input=true; returnG_invalid_input;}BOOLCheckmorethanhalf (int* Numbers,intLenintcandidate) { intTimes =0; for(inti =0; i < Len; i++) if(Numbers[i] = =candidate) times++; if(Times *2<Len) {G_invalid_input=true; returnFasle; } return true;}intPartition (int*numbers,intStartintend) { intPivot =Numbers[end]; intp = Start-1; intj =p; for(; J < End; J + +) { if(Numbers[j] <=pivot) {Swap (a[++p], a[j]); }} swap (a[++p], a[end]); returnp;}intMorethanhalf (int* Numbers,intLen) { if(Checkinvalidarray (Numbers, len))return 0; intK = len/2; intStart =0; intend = Len-1; intpos =Partition (numbers, start, end); while(POS! =k) {if(Pos >k) {End= pos-1; POS=Partition (numbers, start, end); } Else{Start= pos +1; POS=Partition (numbers, start, end); } } intcandidate =Numbers[k]; if(Checkmorethanhalf (numbers, Len, candidate)) candidate=0; returncandidate;}
Number of occurrences more than half in an array