File tree Expand file tree Collapse file tree 2 files changed +15
-15
lines changed Expand file tree Collapse file tree 2 files changed +15
-15
lines changed Original file line number Diff line number Diff line change @@ -76,7 +76,7 @@ public int findNumMoreThanHalf(int[] array) {
76
76
count = 1 ;
77
77
}
78
78
79
- if (array [i ] == result ) count ++;
79
+ else if (array [i ] == result ) count ++;
80
80
else count --;
81
81
}
82
82
Original file line number Diff line number Diff line change @@ -91,20 +91,20 @@ select方法是通用的选择排名为k的元素,只要参数传入n / 2即
91
91
92
92
``` java
93
93
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);
108
108
}
109
109
```
110
110
You can’t perform that action at this time.
0 commit comments