题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
https://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
import java.util.*; public class Solution { public int MoreThanHalfNum_Solution(int [] array) { int rem = 0; int time = 0; for(int i=0; i<array.length; i++){ if(time==0){ rem = array[i]; time =1; } else{ if(array[i]==rem) time++; else time--; } } return rem; } } 感觉是比较简洁的写法,使用思路是选举法。