public class Solution {
public int MoreThanHalfNum_Solution(int [] array) {
int voted = 1;
int res = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] == res) {
voted++;
} else {
voted--;
if (voted == 0) {
res = array[i];
voted++;
}
}
}
return res;
}
}
数组中出现次数超过一半的数字
最新推荐文章于 2025-07-25 15:17:05 发布