1. Majority Element II

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

Note: The algorithm should run in linear time and in O(1) space.

Example 1:

Input: [3,2,3]
Output: [3]

Example 2:

Input: [1,1,1,3,3,2,2,2]
Output: [1,2]

摩尔投票法

回顾求解超过一半的数字。假设超过一半的数字是x,剩下数字的集合记做是Y(\(|Y|<\frac{n}{2}\)),对于任意的\(y_i\in Y\),都能有一个x和其配对,最后还会剩下一个x或者刚好配对完。具体实现为:

实现的算法从第一个数开始扫描整个数组,有两个变量(参考第一答题者的变量名)major和count。其实这两个变量想表达的是一个“隐形的数组”array,array存储的是“当前暂时无法删除的数字”,我们先不要管major和count,只考虑这个array,同时再维护一个结果数组result,result里面存储的是每次删除一对元素之后的当前结果。[]

实现的算法从第一个数开始扫描整个数组,有两个变量(参考第一答题者的变量名)major和count。其实这两个变量想表达的是一个“隐形的数组”array,array存储的是“当前暂时无法删除的数字”,我们先不要管major和count,只考虑这个array,同时再维护一个结果数组result,result里面存储的是每次删除一对元素之后的当前结果。[https://www.zhihu.com/question/49973163/answer/235921864]

对于超过1/3的数字,必然最多有2两个。思路与求1/2的类似

class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
int m, n, cnt_m = 0, cnt_n = 0;
for(int x : nums){
if(m == x){
cnt_m++;
}else if(n == x){
cnt_n++;
}else if(cnt_m == 0){
cnt_m = 1;
m = x;
}else if(cnt_n == 0){
cnt_n = 1;
n = x;
}else{
cnt_m--;
cnt_n--;
}
}
cnt_m = 0, cnt_n = 0;
for(int x: nums){
if(x == m)cnt_m++;
else if(x == n)cnt_n++;
}
vector<int>res;
if(cnt_m > nums.size() / 3)res.push_back(m);
if(cnt_n > nums.size() / 3)res.push_back(n);
return res;
}
};

【刷题-LeetCode】229. Majority Element II的更多相关文章

  1. [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 ...

  2. leetcode 229 Majority Element II

    这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...

  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 ...

  4. Java for LeetCode 229 Majority Element II

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

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

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

  6. (medium)LeetCode 229.Majority Element II

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

  7. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  8. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  9. 【LeetCode】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

随机推荐

  1. CF1292B Aroma's Search 题解

    Content 给定一个坐标系,已知第一个点的坐标为 \((x_0,y_0)\),第 \(i(i>0)\) 个点的坐标满足这样的两个递推式:\(x_i=a_xx_{i-1}+b_x,y_i=a_ ...

  2. ElasticSearch 使用

    一.索引操作 --------------------------------- 创建索引(PUT) PUT /索引名 curl -X PUT http://10.20.20.214:9200/sho ...

  3. .Net Core 项目发布在IIS上 访问404 问题对应

    对策: 1.进入线程池画面,将当前程序的线程池设为"无托管代码"   2.修改配置文件 Web.config,加上配置   原因: 因为.NetCore 5.0 自带集成了Swag ...

  4. Linux(centos7)安装ClickHouse

    Clickhouse 仅支持Linux 且必须支持SSE4.2 指令集 grep -q sse4_2 /proc/cpuinfo && echo "SSE 4.2 suppo ...

  5. Windows系统安装ActiveMQ

    1.下载安装包:https://activemq.apache.org/components/classic/download/ 选择自己的版本进行下载 2.安装JDK 3.把下载的ActiveMQ压 ...

  6. 【九度OJ】题目1169:比较奇偶数个数 解题报告

    [九度OJ]题目1169:比较奇偶数个数 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1169 题目描述: 第一行输入一个数,为n, ...

  7. 【LeetCode】746. Min Cost Climbing Stairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. codeforces626D . Jerry's Protest

    Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds ...

  9. 【项目管理】《IT项目管理》Kathy Schwalbe 第2章 IT项目管理和IT背景

    1.对项目管理采取系统的观点有何意义?如何在项目管理中采用系统的观点? 意义:有效处理复杂的环境 采用系统方法,系统分析,系统管理.2.解释组织的四个框架.他们是如何帮助项目经理理解项目的组织环境的? ...

  10. vue项目发布后,线上运行时刷新404

    修改nginx配置文件 location / { root ... index ... try_files $uri $uri/ /index.html; ---解决页面刷新404问题 } (参考官网 ...