Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
给定size 为n的数组,查找出主元素,就是出现次数大于n/2次的元素。你可以假定数组非空,而且主元素一定存在。
class Solution {
public:int majorityElement(vector<int>& nums) {map<int, int> im;for (int i = 0; i < nums.size(); ++i){map<int, int>::iterator it = im.find(nums[i]);if (it == im.end()) {im[nums[i]] = 1;} else {im[nums[i]]++;}if (im[nums[i]] > nums.size()/2) {return nums[i];}}return 0;}
};
有一种算法叫 Moore’s Voting Algorithm,由Robert S.Boyer 和J Strother Moore于1980年发明,是线性时间复杂度。
int majorityElement(vector<int> &num) {int majority;int cnt = 0;for(int i=0; i<num.size(); i++){if ( cnt ==0 ){majority = num[i];cnt++;}else{majority == num[i] ? cnt++ : cnt --;if (cnt >= num.size()/2+1) return majority;}}return majority;
}
当然,这种算法对于存在主元素的数组是有效的,如:
A A A C C B B C C C B C C
它肯定能返回主元素C。但是,如果不存在主元素,那么得到的结果就跟遍历顺序有关了。如:
A A A C C C B
如果是从左到右,那么结果是B,如果是从右到左,那么结果是A。
class Solution {
public:int majorityElement(vector<int>& nums) {int n = nums.size(); sort(nums.begin(),nums.end()); return nums[n/2]; }
};
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int major(vector<int>);
int main()
{vector<int>a = { 1, 2, 3, 4, 2, 2, 5, 2, 2, 2, 2, 2, 2 };cout << major(a) << endl;system("pause");return 0;}
int major(vector<int> a)
{/*sort(a.begin(), a.end());return a[a.size() / 2];*//*map<int, int> m;for (int i = 0; i < a.size(); i++){if (m.find(a[i]) == m.end()){m[a[i]] = 1;}else{m[a[i]]++;}if (m[a[i]]>a.size() / 2)return a[i];}*///标志位想减法 可以判断出现次数做多数int major;int num = 0;for (int i = 0; i < a.size(); i++){if (num == 0){major = a[i];num++;}else{if (major != a[i])num--;else{num++;}}if (num > a.size() / 2) return major;}}
Majority Element相关推荐
- Majority Element:主元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode】169. Majority Element
原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...
- Leetcode - 169. Majority Element (多数投票问题)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode从零单排】No.169 Majority Element(hashmap用法)
题目 Given an array of size n, find the majority element. The majority element is the element that app ...
- [LeetCode]: 169: Majority Element
题目: Given an array of size n, find the majority element. The majority element is the element that ap ...
- 【LeetCode 169】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
最新文章
- PWM通过RC低通滤波器模拟DAC
- POJ 1837 Balance(01背包变型)
- 基础才是重中之重~DictionaryK,V里V的设计决定的性能
- Git管理工具对比(GitBash、EGit、SourceTree)(转载)
- oracle exacc,【学习笔记】Oracle 11GR2新特性Adaptive Cursor Sharing(ACS)
- Android中打包含有Activity以及资源文件的jar包在工程中调用
- Linux设置环境变量小结:设置永久变量临时变量 全局变量局部变量
- C#.NET验证码智能识别学习笔记---05C#.Net图片预处理
- hdu 2602 Bone Collector 解题报告
- day22 随机输出ArrayList
- Labview之RS485通信
- 多线程(Thread的类的运用-Runnable类的使用/多线程的注意点)
- 自己动手写CPU(8)——简单算术操作指令的实现
- 搜索引擎和网站中的高级搜索技巧
- 微信小程序服务器向客户端发送通知消息,微信小程序消息推送
- FTP服务器是什么意思
- 新手学游戏开发必知的一课
- 不喜欢现在的领导,怎么办?不懂跟领导相处,你到哪都混不好
- 2022蓝桥杯A组Python
- 双向可控硅实现单相交流电机正反转(硬开通加缓冲吸收,无过零)