Tuesday, November 17, 2015

[leetcode] Majority Number

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public class Solution {
    public int majorityElement(int[] nums) {
     if (nums == null || nums.length == 0) return Integer.MIN_VALUE;
        int countCurrent = 0;
        int current = 0;
        for (int i = 0; i < nums.length; i++){
         if (countCurrent == 0) current = nums[i];
         if (current == nums[i]) countCurrent++;
         else countCurrent--;
        }
        return current;
    }
}

No comments:

Post a Comment