题目:

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.

题解:运用多数投票算法的思路来解:从头到尾遍历数组,遇到两个不一样的数就把这两个数同时除去。除去的两个数可能都不是majority,也可能一个是majority一个不是,但是因为majority总数大于一半(注意不能等于一半),所以这么删完了最后肯定剩下的数是majority。(因为题目说了肯定有majority,如果没说,剩下的那个数未必是majority,还应该遍历一遍数组统计这个数的出现次数)。

这个算法的时间复杂度O(n),空间复杂度O(1)

class Solution {
public:
int majorityElement(vector<int>& nums) {
int cnt=,ans=;
for(auto a:nums){
if(cnt==){
ans=a;
cnt=;
}
else if(a==ans){
cnt++;
}
else{
cnt--;
}
}
return ans;
}
};

扩展:如果找改成大于[n/3]的,道理完全一样。参见这道题

leetcode 169. Majority Element 多数投票算法(Boyer-Moore Majority Vote algorithm)的更多相关文章

  1. Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution

    class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...

  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] 229. Majority Element II 多数元素 II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  4. LeetCode OJ 169. Majority Element

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

  5. leetcode 229. Majority Element II(多数投票算法)

    就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...

  6. Moore majority vote algorithm(摩尔投票算法)

    Boyer-Moore majority vote algorithm(摩尔投票算法) 简介 Boyer-Moore majority vote algorithm(摩尔投票算法)是一种在线性时间O( ...

  7. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  8. [LeetCode] Majority Element II 求大多数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  9. Majority Element(169) && Majority Element II(229)

    寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...

随机推荐

  1. 《Head First 设计模式》学习笔记——观察者模式 + 装饰者模式

    装饰者模式是JDK中还有一个使用较多的设计模式,上一个是观察者模式(在Swing中大量使用),业内好的API设计无一离不开常见的设计模式,通常我们所说要阅读源代码,也是为了学习大牛们的设计思路.--- ...

  2. win2012R2无法打开匿名级安全令牌

    解决办法:  1.输入“dcomcnfg.exe”,打开组件服务管理. 2.展开组件服务,计算机,右击我的电脑,选择属性. 3.在默认属性选项卡中,      选择:- 勾选“在此计算机中启用分布式C ...

  3. SpringBoot学习之常用注解

    @SpringBootAppliaction:通常注解写在SpringBoot启动类中,主要包括三个作用: 1.@Configuration表示将该类作用springboot配置文件类. 2.@Ena ...

  4. layui.layer独立组件--解释

    网站文档-链接:http://www.layui.com/doc/modules/layer.html layer至今仍作为layui的代表作,她的受众广泛并非偶然,而是这五年多的坚持,不断完善和维护 ...

  5. 获取input光标的x和y轴

    http://blog.csdn.net/kingwolfofsky/article/details/6586029 index.html <!DOCTYPE html> <html ...

  6. MP4文件格式的解析,以及MP4文件的分割算法

    http://www.cnblogs.com/haibindev/archive/2011/10/17/2214518.html http://blog.csdn.net/pirateleo/arti ...

  7. springboot输出日志到指定目录,简单粗暴,springboot输出mybatis日志

    springboot官方文档地址https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot- ...

  8. Redis专题(2):Redis数据结构底层探秘

    前言 上篇文章Redis闲谈(1):构建知识图谱介绍了redis的基本概念.优缺点以及它的内存淘汰机制,相信大家对redis有了初步的认识.互联网的很多应用场景都有着Redis的身影,它能做的事情远远 ...

  9. php 字符串内容是数组格式 转换成数组

    一个简单的应用.. 例, $str    =    "array( 'USD'=>'1', 'GBP'=>'0.6494', 'EUR'=>'0.7668' ,'JPY'= ...

  10. ios和mac开发 学习资料

    1.WWDC14 Session 409 学习笔记: http://url.cn/Ju2Yt5 2..WWDC14 Session 4092学习笔记: http://url.cn/Rx0mAN 3.i ...