Skip to content

Commit 1243172

Browse files
authored
Update Solution.java
1 parent b24af17 commit 1243172

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/main/java/g0201_0300/s0229_majority_element_ii/Solution.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public List<Integer> majorityElement(int[] nums) {
1111
int second = 1;
1212
int count1 = 0;
1313
int count2 = 0;
14+
// now we have two candidates(any integer can be chosed as),and their votes are
15+
// zero.
1416
for (int i = 0; i < len; i++) {
1517
int temp = nums[i];
1618
if (temp == first) {
@@ -24,9 +26,17 @@ public List<Integer> majorityElement(int[] nums) {
2426
second = temp;
2527
count2++;
2628
} 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.
2732
count1--;
2833
count2--;
2934
}
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.
3040
}
3141
count1 = 0;
3242
count2 = 0;

0 commit comments

Comments
 (0)