题目:

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. mysql索引类型normal,unique,full text

    normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用作索引时,可设置为unique full textl: 表示 全文搜索的索引. FULL ...

  2. Ubuntu Server 安装 NodeJS

    准备命令: $ sudo apt-get install python $ sudo apt-get install build-essential $ sudo apt-get install gc ...

  3. eval(function(p,a,c,k,e,d){e=function(c)加解密

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. cacti 安装和组件添加

    安装cacti 步骤 1.准备lamp环境 2.准备所需包:rrdtool(绘图) cacti(安装程序) net-snmpd(数据收集) 3.安装所需库文件 rrdtool所需库文件有: cairo ...

  5. LRU java实现

    实现LRU缓存,用到了一个链表和 HashMap, HashMap保证了get/set的时间复杂度是O(1), 链表用来记录 最近最少使用的元素,以便用来淘汰. package lru; /** * ...

  6. 图像处理之基础---卷积及其快速算法的C++实现

    头文件: /* * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com * * This program is free so ...

  7. C#利用SharpZipLib进行文件的压缩和解压缩

    我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...

  8. Windows系统下正确安装MongoDB

    1.下载.安装 官网下载: http://www.mongodb.org/downloads 下载好之后,接下来进行安装了: 2.创建数据文件夹 MongoDB将数据文件夹存储在 db 文件夹下. 可 ...

  9. vue+vuex构建单页应用

    基本 构建工具: webpack 语言: ES6 分号:行首分号规则(行尾不加分好, [ , ( , / , + , - 开头时在行首加分号) 配套设施: webpack 全家桶, vue 全家桶 项 ...

  10. 远程服务器上的weblogic项目管理(五) PermGen内存溢出问题

    weblogic偶尔会出现PermGen异常,内存溢出的问题,这个时候需要修改weblogic安装目录下的domain/common/bin/commEnv.cmd. 打开后在其中找到: set ME ...