File tree Expand file tree Collapse file tree 1 file changed +10
-0
lines changed
src/main/java/g0201_0300/s0229_majority_element_ii Expand file tree Collapse file tree 1 file changed +10
-0
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ public List<Integer> majorityElement(int[] nums) {
11
11
int second = 1 ;
12
12
int count1 = 0 ;
13
13
int count2 = 0 ;
14
+ // now we have two candidates(any integer can be chosed as),and their votes are
15
+ // zero.
14
16
for (int i = 0 ; i < len ; i ++) {
15
17
int temp = nums [i ];
16
18
if (temp == first ) {
@@ -24,9 +26,17 @@ public List<Integer> majorityElement(int[] nums) {
24
26
second = temp ;
25
27
count2 ++;
26
28
} else {
29
+ // otherwise,if one of the vote is zero,that's meaning that
30
+ // we only have or even don't have a candidate.So we set the number to the
31
+ // candidate.
27
32
count1 --;
28
33
count2 --;
29
34
}
35
+ // where we have two candidates whose votes bigger than zero,
36
+ // but the current number is not one of them.Votes decrease by 1 and
37
+ // the current number complete its "mission" and is skipped at the same time.
38
+ // once the cycle finished,the target is left after all the counteraction,as its
39
+ // count is bigger than n/3.
30
40
}
31
41
count1 = 0 ;
32
42
count2 = 0 ;
You can’t perform that action at this time.
0 commit comments