简介
如何寻求一个数组中的出现次数最多的书
虽然最开始想到了这个方法但是不知道如何去表达,grep就利用了这个算法
class Solution {
public int majorityElement(int[] nums) {
Integer candidate = null;
int count = 0;
for (int i = 0; i < nums.length; i++) {
if(candidate == null || count == 0) {
candidate = nums[i];
}
if(candidate == nums[i]){
count ++;
}else{
count --;
}
}
return candidate;
}
}
参考链接
https://leetcode.cn/problems/majority-element/solution/duo-shu-yuan-su-by-leetcode-solution/
标签:count,Moore,candidate,nums,int,算法,leetcode,Boyer From: https://www.cnblogs.com/eat-too-much/p/16859395.html