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相关推荐

  1. Majority Element:主元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. leetcode 169. Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. LeetCode Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. [LeetCode] Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. 【LeetCode】169. Majority Element

    原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...

  6. Leetcode - 169. Majority Element (多数投票问题)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. 【LeetCode从零单排】No.169 Majority Element(hashmap用法)

    题目 Given an array of size n, find the majority element. The majority element is the element that app ...

  8. [LeetCode]: 169: Majority Element

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  9. 【LeetCode 169】Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

最新文章

  1. PWM通过RC低通滤波器模拟DAC
  2. POJ 1837 Balance(01背包变型)
  3. 基础才是重中之重~DictionaryK,V里V的设计决定的性能
  4. Git管理工具对比(GitBash、EGit、SourceTree)(转载)
  5. oracle exacc,【学习笔记】Oracle 11GR2新特性Adaptive Cursor Sharing(ACS)
  6. Android中打包含有Activity以及资源文件的jar包在工程中调用
  7. Linux设置环境变量小结:设置永久变量临时变量 全局变量局部变量
  8. C#.NET验证码智能识别学习笔记---05C#.Net图片预处理
  9. hdu 2602 Bone Collector 解题报告
  10. day22 随机输出ArrayList
  11. Labview之RS485通信
  12. 多线程(Thread的类的运用-Runnable类的使用/多线程的注意点)
  13. 自己动手写CPU(8)——简单算术操作指令的实现
  14. 搜索引擎和网站中的高级搜索技巧
  15. 微信小程序服务器向客户端发送通知消息,微信小程序消息推送
  16. FTP服务器是什么意思
  17. 新手学游戏开发必知的一课
  18. 不喜欢现在的领导,怎么办?不懂跟领导相处,你到哪都混不好
  19. 2022蓝桥杯A组Python
  20. 双向可控硅实现单相交流电机正反转(硬开通加缓冲吸收,无过零)

热门文章

  1. Smarty 中的 if 语句条件修饰词
  2. 大道至简 23种模式一点就通
  3. 声腔设计中无前腔的影响
  4. linux下使用update-alternatives切换java版本的正确姿势
  5. springmvc项目在启动完成之后执行一次方法_SpringMVC运行原理
  6. SOFA 源码分析 — 链路数据透传
  7. Lync 2013就地升级到Skype for Business 2015-01
  8. Linux操作系统ssh默认22端口修改方法
  9. chrome 常用快捷键(可以摆脱鼠标哦)
  10. I/O多路转接之 select