From 2bd7df84c73128b82a533a59f3b80fc601e58ab6 Mon Sep 17 00:00:00 2001 From: jinhongliu <1060951934@qq.com> Date: Tue, 19 Oct 2021 10:45:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=89=91=E6=8C=87offer=2039?= =?UTF-8?q?=E9=A2=98=E4=BB=A3=E7=A0=81bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/39. 数组中出现次数超过一半的数字.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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;