LeetCode 229: Majority Element II

Posted 李建明180

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 229: Majority Element II相关的知识,希望对你有一定的参考价值。

Given an integer array of size n, find all elements that appear more than ? n/3 ? times. The algorithm should run in linear time and in O(1) space.
Hint:

  1. How many majority elements could it possibly have?
  2. Do you have a better hint? Suggest it!

题意

思路

这道题让我们求出现次数大于n/3的众数,而且限定了时间和空间复杂度,那么就不能排序,也不能使用哈希表,这么苛刻的限制条件只有一种方法能解了,那就是摩尔投票法 Moore Voting,这种方法在之前那道题Majority Element 求众数中也使用了。题目中给了一条很重要的提示,让我们先考虑可能会有多少个众数。那么有了这个信息,我们使用投票法的核心是找出两个候选众数进行投票,需要两遍遍历,第一遍历找出两个候选众数,第二遍遍历重新投票验证这两个候选众数是否为众数即可,选候选众数方法和前面那篇Majority Element 求众数一样,由于之前那题题目中限定了一定会有众数存在,故而省略了验证候选众数的步骤,这道题却没有这种限定,即满足要求的众数可能不存在,所以要有验证。代码如下:

代码

C语言版本:

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
40
41
42
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* (int* nums, int numsSize, int* returnSize) {
int* res = (int*)malloc(sizeof(int)*numsSize);
int firstNum = 0, secondNum = 0, countFN = 0, countSN = 0;
for(int i=0; i<numsSize; i++){
if (nums[i] == firstNum){
countFN++;
}else if (nums[i] == secondNum){
countSN++;
}else if (countFN == 0){
firstNum = nums[i];
countFN = 1;
}else if (countSN == 0){ 大专栏  LeetCode 229: Majority Element II
secondNum = nums[i];
countSN = 1;
}else{
countFN--;
countSN--;
}
}
countFN = countSN = 0;
for (int i=0; i<numsSize; i++) {
if (nums[i] == firstNum){
++countFN;
}
else if (nums[i] == secondNum){
++countSN;
}
}
if (countFN > numsSize/3){
res[0] = firstNum;
}
if (countSN > numsSize/3){
res[1] = secondNum;
}
*returnSize = numsSize;
return res;
}

C++版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> res;
int m = 0, n = 0, cm = 0, cn = 0;
for (auto &a : nums) {
if (a == m) ++cm;
else if (a ==n) ++cn;
else if (cm == 0) m = a, cm = 1;
else if (cn == 0) n = a, cn = 1;
else --cm, --cn;
}
cm = cn = 0;
for (auto &a : nums) {
if (a == m) ++cm;
else if (a == n) ++cn;
}
if (cm > nums.size() / 3) res.push_back(m);
if (cn > nums.size() / 3) res.push_back(n);
return res;
}
};

以上是关于LeetCode 229: Majority Element II的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode OJ 229. Majority Element II

Leetcode # 169, 229 Majority Element I and II

[LeetCode] 229. Majority Element II 多数元素 II

LeetCode 229. Majority Element II (众数之二)

LeetCode 229 Majority Element II(主要元素II)(Array)(Boyer–Moore majority vote algorithm)

leetcode-Majority Element II-229