修复剑指offer 39题代码bug
This commit is contained in:
parent
456ff183d5
commit
2bd7df84c7
@ -12,10 +12,11 @@
|
|||||||
public int MoreThanHalfNum_Solution(int[] nums) {
|
public int MoreThanHalfNum_Solution(int[] nums) {
|
||||||
int majority = nums[0];
|
int majority = nums[0];
|
||||||
for (int i = 1, cnt = 1; i < nums.length; i++) {
|
for (int i = 1, cnt = 1; i < nums.length; i++) {
|
||||||
cnt = nums[i] == majority ? cnt + 1 : cnt - 1;
|
|
||||||
if (cnt == 0) {
|
if (cnt == 0) {
|
||||||
majority = nums[i];
|
majority = nums[i];
|
||||||
cnt = 1;
|
cnt = 1;
|
||||||
|
} else {
|
||||||
|
cnt = nums[i] == majority ? cnt + 1 : cnt - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user