修复剑指offer 39题代码bug

This commit is contained in:
jinhongliu 2021-10-19 10:45:48 +08:00
parent 456ff183d5
commit 2bd7df84c7

View File

@ -12,10 +12,11 @@
public int MoreThanHalfNum_Solution(int[] nums) {
int majority = nums[0];
for (int i = 1, cnt = 1; i < nums.length; i++) {
cnt = nums[i] == majority ? cnt + 1 : cnt - 1;
if (cnt == 0) {
majority = nums[i];
cnt = 1;
} else {
cnt = nums[i] == majority ? cnt + 1 : cnt - 1;
}
}
int cnt = 0;