1、前言

2、思路
采用打擂台的思路,相同加,不同减
3、代码
class Solution {
public int majorityElement(int[] nums) {
int fre = 0, num = 0;
for(int i = 0; i < nums.length; i++){
if(fre == 0){
num = nums[i];
}
if(num == nums[i]){
fre++;
}else{
fre--;
}
}
return num;
}
}
网友评论