剑指 Offer 39. 数组中出现次数超过一半的数字

剑指 Offer 39. 数组中出现次数超过一半的数字

数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。

示例 1:

输入: [1, 2, 3, 2, 2, 2, 5, 4, 2]
输出: 2

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
    public int majorityElement(int[] nums) {
        int count = 0;
        int ans = nums[0];

        for(int i=0; i<nums.length; i++){
            if(count == 0) ans = nums[i];   //如果票数为0,重新赋给ans
            if(ans == nums[i]){     //如果遇到相同的,票数++
                count++;
            }else{
                count--;
            }
        }
        return ans;    //最终就是票数得到最多的
    }
}
posted @ 2021-01-04 09:00  xiaoff  阅读(62)  评论(0编辑  收藏  举报