题解 | #数组中出现次数超过一半的数字#

数组中出现次数超过一半的数字

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;
}

}

全部评论

相关推荐

07-01 23:23
郑州大学 Java
否极泰来来来来:牛客迟早有高三的
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-16 18:03
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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