目录标题
- [2023-8-9 10:43:27](#2023-8-9 10:43:27)
2023-8-9 10:43:27
java
public int majorityElement(int[] nums) {
int minTimes = nums.length / 2;
Arrays.sort(nums);
int pre = 0;
int last = pre;
while (last < nums.length) {
if (nums[pre] != nums[last]) {
if (last - pre > minTimes) {
return nums[pre];
} else {
pre = last;
}
}
last++;
}
return nums[pre];
}
摩尔投票法:在一个群体里面,哪个数最多