-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmajority-element-ii.ts
39 lines (34 loc) · 1.01 KB
/
majority-element-ii.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* https://leetcode.com/problems/majority-element-ii/description/
*/
/**
* 多数投票算法
*/
// [1,1,1,3,3,2,2,2]
const majorityElementII = nums => {
// candidate1、candidate2 设置不同的初始值
let [count1, count2, candidate1, candidate2] = [0, 0, 0, 1];
for (let i = 0; i < nums.length; i++) {
if (candidate1 === nums[i]) {
count1 += 1;
} else if (candidate2 === nums[i]) {
count2 += 1;
} else if (count1 === 0) {
[candidate1, count1] = [nums[i], 1];
} else if (count2 === 0) {
[candidate2, count2] = [nums[i], 1];
} else {
[count1, count2] = [count1 - 1, count2 - 1];
}
}
// 检测 candidate1, candidate2 是否满足条件
[count1, count2] = [0, 0];
for (let i = 0; i < nums.length; i++) {
if (candidate1 === nums[i]) count1 += 1;
if (candidate2 === nums[i]) count2 += 1;
}
const result = [];
if (count1 > nums.length / 3) result.push(candidate1);
if (count2 > nums.length / 3) result.push(candidate2);
return result;
};