47 Majority Element II
原题网址; https://www.lintcode.com/problem/majority-element-ii/
描述
给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一。
数组中只有唯一的主元素
样例
给出数组[1,2,1,2,1,3,3] 返回 1
挑战
要求时间复杂度为O(n),空间复杂度为O(1)。
查看标签
枚举法
class Solution {
public:
/*
* @param nums: a list of integers
* @return: The majority number that occurs more than 1/3
*/
int majorityNumber(vector<int> &nums) {
// write your code here
int n=nums.size();
map<int,int> m;
for (int i=;i<n;i++)
{
m.insert(pair<int,int>(nums[i],));
}
for (int i=;i<n;i++)
{
m[nums[i]]++;
}
for (int i=;i<n;i++)
{
if (m[nums[i]]>n/)
{
return nums[i];
}
}
}
};
另一种方法:摩尔投票法 可参照:摩尔投票法 https://blog.csdn.net/krystalhuang/article/details/71512901 以及 https://www.cnblogs.com/ZhangYushuang/p/4627503.html
思路:
1, 超过n/3的元素个数最多两个
2, 数组中连续3个数据为一组的话,一共n/3组,那么如果存在符合条件的元素,这个元素一定出现在某一个组内至少两次
3, 知道了以上两个条件后,用所谓的摩尔投票法,共两轮,
第一轮:找出出现次数最多的两个元素,每次存储两个元素n1和n2【注意n1 != n2】,如果第三个元素与其中一个相同,则增加计数cn1或cn2, 否则,清除n1和n2,从下一个数据开始重新统计.
根据第二条,数目较多的元素一定可以被查到,当然查到的不一定是大于n/3的.【有可能是连续3个为一组某个数字出现两~三次】
第二轮:验证这两个元素是否满足条件,即出现的次数是否大于n/3;
class Solution {
public:
/*
* @param nums: a list of integers
* @return: The majority number that occurs more than 1/3
*/
int majorityNumber(vector<int> &nums) {
// write your code here
int n=nums.size();
int candidate1,candidate2,count1=,count2=;
for (int i=;i<n;i++)
{
if (count1==)
{
candidate1=nums[i];
count1=;
}
else if (count2==&&nums[i]!=candidate1) //注意此处的nums[i]!=candidate1,两个candidate不能是同一个数;
{
candidate2=nums[i];
count2=;
}
else if (nums[i]==candidate1)
{
count1++;
}
else if (nums[i]==candidate2)
{
count2++;
}
else
{
count1--;
count2--;
}
}
count1=count2=;
for (int i=;i<n;i++)
{
if (nums[i]==candidate1)
{
count1++;
}
if (nums[i]==candidate2)
{
count2++;
}
}
if (count1>n/)
{
return candidate1;
}
if (count2>n/)
{
return candidate2;
}
}
};
47 Majority Element II的更多相关文章
- 47.Majority Element I & II
Majority Element I 描述 给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一. You may assume that the array is non ...
- LeetCode(169)Majority Element and Majority Element II
一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...
- Majority Element,Majority Element II
一:Majority Element Given an array of size n, find the majority element. The majority element is the ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- Majority Element(169) && Majority Element II(229)
寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...
- 【LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III
LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...
- 【刷题-LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
随机推荐
- ps制作浮雕和投影效果
1用文字横排工具写个hope,按住ctrl+t可以调试出文字工具,上面直接用500点来改变文字的大小.2 用矩形选框工具直接可以切割图片的大小,然后双击一个图层,添加样式为浮雕....然后合并图层3 ...
- (十三)在ASP.NET CORE中使用Options
这一节介绍Options方法,继续在OptionsBindSample项目下. 在项目中添加一个Controllers文件夹,文件夹添加一个HomeController控制器 HomeControll ...
- linux 下安装maven私服
1.下载最新nexus安装包 下载地址:https://www.sonatype.com/download-oss-sonatype 2.下载JDK7,nexus2版本支持JDK7:nexus1版本支 ...
- 2019-9-11-在-P2P-文件分享应用以文件或文件段为单位的优缺
title author date CreateTime categories 在 P2P 文件分享应用以文件或文件段为单位的优缺 lindexi 2019-09-11 10:23:27 +0800 ...
- mantis 添加新状态配置方法
在mantis的状态栏中一般只有:新建.反馈.认可.已确认.已分派.已解决.已关闭,七个选项,如果想在其中加入新的状态怎么做? 我要加入的状态为:重新打开 1.添加状态信息 打开config_defa ...
- git统计项目中成员代码量
查看git上个人代码量 git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; su ...
- EF 如何更新多对多关系的实体
ctx.Entry(user).Collection(t => t.UserPrivileges).Load(); Come form:https://www.thereformedprogra ...
- day26 re正则表达式
Python之路,Day14 = Python基础14 compile() match() search() findall() m.group() # 括号里面剋跟参数,表示打印里面(分组)的第几 ...
- flask中abort()函数的使用
一.介绍 #从flask中导入abort from flask import abort abort()函数的作用 可以让开发者在检测到web访问错误时,立即将错误信息返回回去,返回的错误码必须是已知 ...
- error C2712: Cannot use __try in functions that require object unwinding
转自VC错误:http://www.vcerror.com/?p=52 问题描述: error C2712: Cannot use __try in functions that require ob ...