方法三你写复杂了: public class Solution { public int MoreThanHalfNum_Solution(int [] tmp) { int n = tmp.length; int num = -1, cnt = 0; for (int i = 0; i < n; i++) { if (tmp[i] == num) { cnt++; } else { if (cnt == 0) { num = tmp[i]; } else { cnt--; } } } return num; } }
点赞

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务