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 ...
随机推荐
- 【数论】[SDOI2010]古代猪文
大概就是求这个: $$G^\sum_{k|N} C_{n}^{k}$$ 显然只要把后面的$\sum_{k|N}C_{n}^{k}$求出来就好了 几个要用的定理: 欧拉定理的推论:(a和n互质) $$a ...
- Fence Obstacle Course
Fence Obstacle Course 有n个区间自下而上有顺序的排列,标号\(1\sim n\),第i个区间记做\([l_i,r_i]\),现在从第n个区间的起点s出发(显然s在\([l_n,r ...
- PHP微信公众号支付弹出“NaN:undefined”解决方法
最近研究php微信支付开发,从微信官方下载了微信支付的demo后,测试时总是弹出NaN:Undefined这样的错误提示.不过这个并不影响支付的操作,支付成功后依然可以正常获取到支付状态,并进行跳转. ...
- flask 使用hashlib加密
flask 使用hashlib加密 import hashlib #引入hashlib #使用方法: password = ' sha1 = hashlib.sha1() #使用sha1加密方法,你还 ...
- Vim模糊查找与替换
例如要把 ( 1 ).( 2 ) - 全部替换成其他字符,可以用命令: :%s/(.*)/str/gn 其中,.* 表示匹配任何东西,如果只希望匹配应为字母和数字,可以用 \w\+. 有些特殊字符需要 ...
- 廖雪峰Java16函数式编程-2Stream-2创建Stream
1. 方法1:把一个现有的序列变为Stream,它的元素是固定的 //1.直接通过Stream.of()静态方法传入可变参数进行创建 Stream<Integer> s = Stream. ...
- pycharm中使用配置好的virtualenv环境,自动生成和安装requirements.txt依赖
1.手动建立: 第一步 建立虚拟环境 Windows cmd: pip install virtualenv 创建虚拟环境目录 env 激活虚拟环境 C:\Python27\Scripts\env\S ...
- VC++ COMBO BOX控件的使用
1.你在编辑状态下点那个控件的向下的三角形,就出冒出来一个可以调高度的东东.将高度调高,否则在执行时会不能显示下拉选项. 2.为combo box添加选项,在编辑状态下选combo box控件的属 ...
- spring中使用RabbitMQ
常见的消息中间件产品: (1)ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS P ...
- hibernate_01_SSH环境搭建
1.maven工程pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="h ...