题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
public class Solution { public int MoreThanHalfNum_Solution(int [] array) { int result = 0; int record = array[0]; int size = array.length - 1; for(int i = 1;i < size ;i ++){ // if(result == 0){ // record = array[i]; // }
if(record == array[i]){
result ++;
}
else if(result > 0){
result --;
}
if(result == 0){
record = array[i + 1];
}
}
return record;
}
}