给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 你的算法应该在O(1)空间中以线性时间运行。

详见:https://leetcode.com/problems/majority-element-ii/description/

摩尔投票法 Moore Voting

Java实现:

class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> res=new ArrayList<Integer>();
if(nums==null||nums.length==0){
return res;
}
int a=nums[0],cnta=0;
int b=nums[0],cntb=0;
for (int num : nums) {
if (num == a) {
++cnta;
}else if(num == b) {
++cntb;
}else if(cnta == 0) {
a = num;
cnta=1;
}else if(cntb == 0) {
b = num;
cntb=1;
}else{
--cnta;
--cntb;
}
}
cnta=0;
cntb=0;
for(int num:nums){
if(num==a){
++cnta;
}else if(num==b){
++cntb;
}
}
if(cnta>nums.length/3){
res.add(a);
}
if(cntb>nums.length/3){
res.add(b);
}
return res;
}
}

C++实现:

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 = 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;
}
};

参考:https://www.cnblogs.com/grandyang/p/4606822.html

229 Majority Element II 求众数 II的更多相关文章

  1. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  2. 229. Majority Element II求众数II

    网址:https://leetcode.com/problems/majority-element-ii/ 参考:https://blog.csdn.net/u014248127/article/de ...

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

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

  4. 229. 求众数 II

    Q: 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: [3 ...

  5. Java实现 LeetCode 229 求众数 II(二)

    229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...

  6. Leetcode 229.求众数II

    求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

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

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

随机推荐

  1. 微信小程序-setData()方法

    一般setData方法多用于点击后改变页面信息或者刷新后与后台交互获取最新的信息 注意: 直接修改 this.data 而不调用 this.setData 是无法改变页面的状态的,还会造成数据不一致 ...

  2. Wi-Fi无线网络(WPA2加密)快速破解 ——某公司无线设备安全隐患报告

    Wi-Fi无线网络(WPA2加密)快速破解 --某公司无线设备安全隐患报告 评估人:阿牛 2013年12月12日 文件夹 一. 导言 2 二. 背景 2 三. 无线产品应当採取的安全策略 3 四. 存 ...

  3. 2016/3/24 ①数据库与php连接 三种输出fetch_row()、fetch_all()、fetch_assoc() ②增删改时判断(布尔型) ③表与表之间的联动 ④下拉菜单 ⑤登陆 三个页面

    ①数据库与php连接   图表 header("content-type:text/html;charset=utf-8"); //第一种方式: //1,生成连接,连接到数据库上的 ...

  4. C项目实践之通讯录管理案例

    1.功能需求分析 通讯录管理案例主要实现对联系人的信息进行添加.显示.查找.删除.更新和保存功能.主要功能需求描述如下: (1)系统主控平台: 充许用户选择想要进行的操作,包括添加联系人信息,显示.查 ...

  5. how to use datatables editor

    Basic initialisation Editor is a Create, Read, Update and Delete (CRUD) extension forDataTables that ...

  6. 在做java 的web开发,为什么要使用框架

    现在做项目都会使用框架,现在很常见的框架就是SSH(Struts+SpringMVC+spring+hibernate),SSM(Struts/springMVC+Spring+Hibernate), ...

  7. JavaScript 实现的 SHA1 散列

    1.代码:/****  Secure Hash Algorithm (SHA1)*  http://www.webtoolkit.info/***/  function SHA1 (msg) {    ...

  8. BZOJ_2081_[Poi2010]Beads_哈希

    BZOJ_2081_[Poi2010]Beads_哈希 Description Zxl有一次决定制造一条项链,她以非常便宜的价格买了一长条鲜艳的珊瑚珠子,她现在也有一个机器,能把这条珠子切成很多块(子 ...

  9. 【HNOI2004】 打鼹鼠

    [题目链接] 点击打开链接 [算法] 动态规划 f[i]表示上一次打了第i只鼹鼠,所能打死的最多的鼹鼠数量 [代码] #include<bits/stdc++.h> using names ...

  10. appium学习【五】【转】appium实现屏幕向左滑动

    转自http://www.cnblogs.com/testhub/p/5949668.html 前些日子写一个滑动手机页面的小脚本,看到大家给的内容都是swipe方法,这里对swipe方法做一个小介绍 ...