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.

题目大意:给定一个大小为n的数组,找出Majority元素,满足出现次数大于 ⌊ n/3 ⌋ 次。

解题思路:大于 ⌊ n/3 ⌋ 次,那么应该最多有两个Majority元素,之前有道题是找出 大于⌊ n/2 ⌋ 次Majority元素,这道题有点类似,稍微复杂一点,还是利用投票算法,count作为对元素的计数,候选元素等于当前元素则count+1,否则减1,count等于0则更换候选元素。

    public List<Integer> majorityElement(int[] nums) {
List<Integer> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
int can1 = nums[0], can2 = nums[0], cnt1 = 1, cnt2 = 0;
for (int i = 1; i < nums.length; i++) {
int num = nums[i];
if ((cnt1 == 0 && num != can2)) {
can1 = num;
} else if (cnt2 == 0 && num != can1) {
can2 = num;
}
if (num == can1) {
cnt1++;
} else if (num == can2) {
cnt2++;
} else {
cnt1--;
cnt2--;
}
cnt1 = cnt1 < 0 ? 0 : cnt1;
cnt2 = cnt2 < 0 ? 0 : cnt2;
}
cnt1 = 0;
cnt2 = 0;
for (int num : nums) {
if (num == can1) {
cnt1++;
} else if (num == can2) {
cnt2++;
}
}
if (cnt1 > nums.length / 3)
res.add(can1);
if (cnt2 > nums.length / 3)
res.add(can2);
return res;
}

Majority Element II——LeetCode的更多相关文章

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

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

  2. LeetCode(169)Majority Element and Majority Element II

    一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...

  3. 【LeetCode】229. Majority Element II

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

  4. 【刷题-LeetCode】229. Majority Element II

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

  5. Majority Element,Majority Element II

    一:Majority Element Given an array of size n, find the majority element. The majority element is the ...

  6. Majority Element(169) && Majority Element II(229)

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

  7. LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III

    LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...

  8. [LeetCode] Majority Element II 求众数之二

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

  9. 【LeetCode 229】Majority Element II

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

随机推荐

  1. ASP.NET Boilerplate Castle容器无缝添加日志功能

    以添加log4net日志框架为例进行讲解 1.通常log4net的配置参数放在单独的配置文件中,但也可以写在web.config中,这里在我们的web项目中添加log4net.config应用配置文件 ...

  2. Spire.Barcode好用的条码生在组件

    由于项目的需要,今天在网上找了一下条码的组件,发现了一个简单易用的组件,使用简单,几句代码就搞定了.

  3. 利用DIV,实现简单的网页布局

    <html lang="en"><head> <meta charset="UTF-8"> <title>GIS ...

  4. 传统的log4j实战

    /** * */ package log4j; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator ...

  5. 创建 window service 定时任务

    参考文章:http://www.cnblogs.com/jack-liang/archive/2011/05/20/2051743.html 前段时间做过一个项目,前端系统提供添加定时任务,后端系统要 ...

  6. linux 脚本编写基础(一)

    1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在 ...

  7. Chrome调试(转)

    原文地址:http://blog.csdn.net/chenmoquan/article/details/44943245#comments 觉得写的很适合web开发的新手 Chrome 的开发者工具 ...

  8. 关于css float 属性以及position:absolute 的区别。

    1.float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素.div一个典型的块 ...

  9. tab选项卡-jQuery

    上次用原生的js写了个tab选项卡   这次按照一样的思路用jQuery写了一个 ,直接看代码: /*布局*/ <div id="div1"> <input cl ...

  10. Dedecms自定义sql 出现错误Safe Alert: Request Error step 2!

    Dedecms自定义执行sql: SELECT body FROM dede_addonarticle WHERE aid = (select max(aid) fromdede_addonartic ...