Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.

Hint:

  1. How many majority elements could it possibly have?
  2. Do you have a better hint? Suggest it!
解法一:
Moore voting的方法。
class Solution {
public:vector<int> majorityElement(vector<int>& nums) {vector<int> res;int m=0, n=0, cm=0, cn=0;for(auto &a:nums){if(a==m) ++cm;else if(a==n) ++cn;else if(cm==0) m=a, cm=1;else if(cn==0) n=a, cn=1;else --cm, --cn;}cm=0; cn=0;for(auto &a:nums){if(a==m) cm++;else if(a==n) cn++;}if(cm>nums.size()/3) res.push_back(m);if(cn>nums.size()/3) res.push_back(n);return res;}
};

[leetcode] 229. Majority Element II相关推荐

  1. LeetCode 229 : Majority Element II

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

  2. leetcode 229. Majority Element II | 229. 求众数 II(找出现次数超过n/k的元素)

    题目 https://leetcode.com/problems/majority-element-ii/ 题解 思路来源于左程云<程序员代码面试指南> 问题描述 原问题:给定一个整型数组 ...

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

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

  4. 229. Majority Element II**

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

  5. 229. Majority Element II 【M】【52】

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

  6. 229. Majority Element II

    class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :r ...

  7. [LeetCode] 169. Majority Element 多数元素

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

  8. Majority Element(169) Majority Element II(229)

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

  9. [LeetCode] Majority Element II

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

最新文章

  1. 新版中青——青龙羊毛
  2. Entity Framework中IQueryable, IEnumerable, IList的区别
  3. 为什么说一次一密加是密抗窃听无条件安全的?
  4. java中实现将十六进制按位取反
  5. mysql cmd链接不上数据库情况汇总
  6. python读取配置文件获取所有键值对_python读取配置文件
  7. 测试低频噪音软件,设计制作并验证0.1Hz10Hz超低频微弱噪音检测放大器要点
  8. Leetcode 235.二叉搜索树的最近公共祖先
  9. python pandas库的应用(类比mysql语言)
  10. 什么时候以及为什么基于树的模型可以超过神经网络模型?
  11. InvalidateRect的困惑
  12. linux下rsync服务的搭建
  13. CodeForces - 976C(结构体排序思维)
  14. mysql dbutil_通过dbutil操作数据库
  15. cad道路里程桩号标注_怎么用CAD标注道路桩号?
  16. 不能创建对象qmdispatch_按键精灵更新时提示 ActiveX 部件不能创建对象 错误代码 800a01ad_电脑故障...
  17. word2016排版脚注问题
  18. 七参数坐标转换的几种解法
  19. windows下气象grib/grib2数据转Tiff格式输出
  20. [转]条形码基本知识教程 Code39码

热门文章

  1. DAO赛道异军突起,M-DAO的优势在哪里?
  2. 读书百客:《题竹林寺》简析
  3. 1660s功耗多少w_gtx660满载功耗是多少
  4. RGB YUV [转]
  5. Zhong__Centos8安装docker
  6. 如何高效学习_斯科特扬(读后,整体学习法试用,总结)
  7. 【校招 --阶段一 系统编程】system V共享内存
  8. SpringBoot接入支付宝
  9. HTTPS、证书机构(CA)、证书、数字签名、私钥、公钥
  10. level升级打怪是什么意思_【信好有你】LEVEL+1 升级打怪