image.png

解题思路

哈希表

Boyer-Moore 投票算法

image.png

  1. int count = 0;
  2. Integer candidate = null;
  3. for(int num:nums){
  4. if(count == 0){
  5. candidate = num;
  6. }
  7. count += (num == candidate)?1:-1;
  8. }
  9. return candidate;