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 hereint n=nums.size();map<int,int> m;for (int i=0;i<n;i++){m.insert(pair<int,int>(nums[i],0));}for (int i=0;i<n;i++){m[nums[i]]++;}for (int i=0;i<n;i++){if (m[nums[i]]>n/3){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 hereint n=nums.size();int candidate1,candidate2,count1=0,count2=0;for (int i=0;i<n;i++){if (count1==0){candidate1=nums[i];count1=1;}else if (count2==0&&nums[i]!=candidate1) //注意此处的nums[i]!=candidate1,两个candidate不能是同一个数; {candidate2=nums[i];count2=1;}else if (nums[i]==candidate1){count1++;}else if (nums[i]==candidate2){count2++;}else{count1--;count2--;}}count1=count2=0;for (int i=0;i<n;i++){if (nums[i]==candidate1){count1++;}if (nums[i]==candidate2){count2++;}}if (count1>n/3){return candidate1;}if (count2>n/3){return candidate2;}} };
转载于:https://www.cnblogs.com/Tang-tangt/p/9089697.html
47 Majority Element II相关推荐
- Majority Element(169) Majority Element II(229)
寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法) 1.Majority Element 1)description Given an array of s ...
- [LeetCode] Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- LeetCode 229 : Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- LeetCode Majority Element II(Moore Voting Algorithm即Majority Voting Algorithm)
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algo ...
- Majority Element II
https://leetcode.com/problems/majority-element-ii/#/description 挑出所有大于n/3 的数,两个很相似的题,但是这次的major 是> ...
- leetcode 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public:vector<int&g ...
- [Swift]LeetCode229. 求众数 II | Majority Element II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 229. Majority Element II**
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [leetcode] 229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
最新文章
- HDU 2564 饭卡
- 3.1、Eclipse
- 单元测试之Mockito与PowerMock
- 【2016年第1期】从政策驱动到技术践行:大数据开辟可持续发展研究新途径
- C#+.Net使用RemObjects建立客户端服务端
- struts2之拦截器详解
- 哈夫曼编码C++实现
- 【Azure 应用服务】App Service 开启了私有终结点(Private Endpoint)模式后,如何来实现公网Git部署呢?
- Java不生成 xml注解_java注解生成xml和包含CDATA
- Swipe轮播插件的使用
- Absible#Ansible-Playbook的任务控制
- vue3关闭语法检测
- 【探花交友DAY 08】左滑不喜欢右滑喜欢以及附近的人
- Kali Linux系统正确安装指南教程(一)MAC安装kail+Vmware Fusion详细教程(吐血本人测试10次)
- 微信朋友圈为什么不能发gif图?
- vue中image 的:src 与 src
- [乐意黎原创]PHP启用session后抛 session_start(): open(/var/lib/php/session/sess_... 的解决办法
- cxf 实名认证---全国公民身份信息系统
- 编写三个系别的学生类:英语系,计算机系,文学系(要求通过继承学生抽象类)各系来显示以下成绩:
- “熊猫烧香”变本加厉 当心“金猪报喜”
热门文章
- Sparse Modeling of Intrinsic Correspondences
- 使用Pix For Windows调试Shader傻瓜教程
- 一种结合颜色特征和区域生长的疾病斑图像分割方法(复杂环境下分割效果好)
- keras下的线性回归实例
- java遍历范型list_Java 集合(1)-- 俯瞰 Java 集合源码以及分类
- 看漫画学python_《看漫画学Python:有趣、有料、好玩、好用(全彩版)(博文视点出品)》(关东升)【摘要 书评 试读】- 京东图书...
- 输出高电平程序c语言,51用c语言怎么编程检测US-100超声波测距模块echo/rx引脚输出高电平时间长度,,谁能给我个模板...
- ObjC学习11-复制对象与归档
- python在生活中有哪些用处-Python语言有哪些用途
- 多线程并发-java内存模型和计算机基础