核心思想就是1对1消除,我和你不一样,那么我和你都消除,有点类似于猜拳,到最后我多,剩余的肯定是我的数字
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) { if(nums == null || nums.length == 0) { return 0; } int moreThanHalf = 0 ; int count = 0 ; for(int i=0; i<nums.length ; i++) { if(count == 0) { moreThanHalf = nums[i] ; count ++ ; }else { if(moreThanHalf == nums[i]) { count ++ ; } else { count -- ; if(count == 0) { moreThanHalf = 0 ; } } } } return moreThanHalf ; } }