169. Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

一刷
题解:
由于appears more than ⌊ n/2 ⌋ times, 用count变量计算当前值出现次数,如果如果num[I]与当前值不等,count--, 如果count小于0,改变当前值,count归零。

public class Solution {
    public int majorityElement(int[] nums) {
        int major = nums[0], count = 1;
        for(int i=1; i

你可能感兴趣的:(169. Majority Element)