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. CF24B F1 Champions 题解

    Content 有 \(n\) 场已经进行完的赛车比赛,每场比赛给出前 \(m\) 名的名字.在每场比赛中,前 \(10\) 名的选手分别可以获得 \(25,18,15,12,10,8,6,4,2,1 ...

  2. JAVA微信公众号网页开发——通过接收人的openid发送模板消息

    TemplateData.java 1 package com.weixin.weixin.template; 2 3 public class TemplateData { 4 private St ...

  3. Caused by: redis.clients.jedis.exceptions.JedisDataException: READONLY You can't write against a read only slave.

    Caused by: redis.clients.jedis.exceptions.JedisDataException: READONLY You can't write against a rea ...

  4. Qt5使用QSqlQuery读写sqlite3数据库

    概述 本文将介绍使用 Qt5使用QSqlQuery读写sqlite3. 设计初衷: 项目需要使用配置文件,配置文件使用的是sqlite3 , 这是V1.0.0, 后期增加其他功能. 需要C++11支持 ...

  5. 【LeetCode】1007. Minimum Domino Rotations For Equal Row 解题报告(Python)

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

  6. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

  7. 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)

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

  8. 1007 - Mathematically Hard

    1007 - Mathematically Hard    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 6 ...

  9. bugku的一道代码审计基础题:eval

    首先看到 include "flag.php",第一反应就应该是文件包含 直接先?hello=file:////etc, 然后啥也没 那就再检查一下代码,eval(var_dump ...

  10. Go package(3):io包介绍和使用

    IO 操作的基本分类 在计算机中,处理文件和网络通讯等,都需要进行 IO 操作,IO 即是 input/ouput,计算机的输入输出操作. Go语言中的 IO 操作封装在如下几个包中: io 为 IO ...