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. java 图形化小工具Abstract Window Toolit ImageIO缩放图片,添加水印

    实现步骤: 读取图像Image src = ImageIO.read 创建目标图像BufferedImage distImage = new BufferedImage(dstWidth, dstHe ...

  2. call this的范围

    var f1=function(){this.a="类f1的实例的a属性"}; f1代表一个类: f1.a='对象f1的a属性'; var f2=function(){};//类f ...

  3. flink启动报错:java.lang.NoSuchMethodError: org.apache.flink.api.java.ClosureCleaner.clean(Ljava/lang/Object;Z)V

    这是因为pom文件引入的flink相关依赖设置成了provided 作用域 要嘛把这个去掉 但是如果是IDEA的话 可以更简单的操作 把这个打勾保存 接着重新启动main方法即可

  4. 【九度OJ】题目1182:统计单词 解题报告

    [九度OJ]题目1182:统计单词 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1182 题目描述: 编一个程序,读入用户输入的,以 ...

  5. 【LeetCode】717. 1-bit and 2-bit Characters 解题报告(Python)

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

  6. 【剑指Offer】合并两个排序的链表 解题报告(Python)

    [剑指Offer]合并两个排序的链表 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-interview ...

  7. 常见分布式唯一ID生成策略

    方法一: 用数据库的 auto_increment 来生成 优点: 此方法使用数据库原有的功能,所以相对简单 能够保证唯一性 能够保证递增性 id 之间的步长是固定且可自定义的 缺点: 可用性难以保证 ...

  8. 【python】QT5 cvimg 转 pixmap

    自己乱搞了一个 import cv2 from PIL import Image imageRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) im = Image. ...

  9. Feign动态调用,结合Ribbon

    代码如下,三种方法: import org.springframework.beans.factory.annotation.Autowired;import org.springframework. ...

  10. Entropy, relative entropy and mutual information

    目录 Entropy Joint Entropy Conditional Entropy Chain rule Mutual Information Relative Entropy Chain Ru ...