码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 229: Majority Element II

时间:2020-03-01 14:18:51      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:信息   word   you   www   就是   fir   return   href   使用   

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

标签:信息   word   you   www   就是   fir   return   href   使用   

原文地址:https://www.cnblogs.com/lijianming180/p/12389261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!