解题思路:
有点类似于Boyer-Moore 投票算法,但更加形象。
class Solution {public int majorityElement(int[] nums) {int winner = nums[0];int cnt = 1;for (int i = 1; i < nums.length; i++) {if (winner == nums[i]){cnt++;} else if (cnt == 0) {winner = nums[i];cnt++;} else {cnt--;}}return winner;}
}