Skip to content

Commit 528cd88

Browse files
committed
修改了面试题39的代码
1 parent 4f305b5 commit 528cd88

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

code/offer/src/Chap5/MoreThanHalf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public int findNumMoreThanHalf(int[] array) {
7676
count = 1;
7777
}
7878

79-
if (array[i] == result) count++;
79+
else if (array[i] == result) count++;
8080
else count--;
8181
}
8282

notes/剑指offer面试题39--数组中出现次数超过一半的数字.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,20 @@ select方法是通用的选择排名为k的元素,只要参数传入n / 2即
9191

9292
```java
9393
public int findNumMoreThanHalf(int[] array) {
94-
if (array == null || array.length == 0) return 0;
95-
96-
int count = 1;
97-
int result = array[0];
98-
for (int i = 1; i < array.length; i++) {
99-
if (count == 0) {
100-
result = array[i];
101-
count = 1;
102-
}
103-
104-
if (array[i] == result) count++;
105-
else count--;
106-
}
107-
return checkMoreThanHalf(array, result);
94+
if (array == null || array.length == 0) return 0;
95+
96+
int count = 1;
97+
int result = array[0];
98+
for (int i = 1; i < array.length; i++) {
99+
if (count == 0) {
100+
result = array[i];
101+
count = 1;
102+
}
103+
104+
else if (array[i] == result) count++;
105+
else count--;
106+
}
107+
return checkMoreThanHalf(array, result);
108108
}
109109
```
110110

0 commit comments

Comments
 (0)