diff --git a/notes/39. 数组中出现次数超过一半的数字.md b/notes/39. 数组中出现次数超过一半的数字.md index 50877830..4b21ab2c 100644 --- a/notes/39. 数组中出现次数超过一半的数字.md +++ b/notes/39. 数组中出现次数超过一半的数字.md @@ -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;