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. Java使用poi对Execl简单_读_操作

    public class ReadExecl { // private final String XLSX = ".xlsx"; // 2007以上版本 // private fi ...

  2. 重载,重写和super

    1.重载的概念:----->在同一个类中,允许存在同名函数,但它们的参数个数或者参数类型不同即可.public static void main(String[] args){System.ou ...

  3. oracle约束条件状态

    Oracle完整性约束有一下4种: • DISABLE NOVALIDATE • ENABLE NOVALIDATE • DISABLE VALIDATE • ENABLE VALIDATE   •  ...

  4. StudioStyle 使用 厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试

    厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试 http://studiostyl.es/ 去下载个自己喜欢的编码样式吧 如果你有想法 有能力 可以自己去做一个自己喜欢的  OK ...

  5. IE浏览器设置

  6. RAC配置、安装

    RAC  配置及安装 2012年12月30日 星期日 21:49 ******************************************************************* ...

  7. Debug your C# project more efficiently

    I am a very beginner working with C# and Visual Studio 2013. When I debug my project, I always reope ...

  8. IE6 png兼容问题

    1.IE6 png  <!--[if IE 6]>  <script src="../js/png.js" type="text/javascript& ...

  9. Facade 模式

    在软件系统开发中经常回会遇到这样的情况,你实现了一些接口(模块),而这些接口(模块)都分布在几个类中(比如 A和 B.C.D) :A中实现了一些接口,B 中实现一些接口(或者 A代表一个独立模块,B. ...

  10. Queue学习

    Queue在Python中可以算作是一种容器,但是他和list,set,dict不一样. 1. Queue不是Python内置类型.它在Queue模块中定义. 2. 它不是iterator容器,他不能 ...