面试题17.10 主要元素
class Solution {public int majorityElement(int[] nums) {int i, c, count;c = nums[0];count = 1;for (i = 1; i < nums.length; i++) {if (nums[i] == c) {count += 1;}else {if (count > 0) {count -= 1;} else {c = nums[i];count = 1;}}}count = 0;for (i = 0; i < nums.length; i++) {if (nums[i] == c)count += 1;}if (count > nums.length / 2 )return c;elsereturn -1;}}
