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.

解题思路:

《编程之美》寻找发帖水王的原题,两次遍历,一次遍历查找可能出现次数超过nums.length/3的数字,(出现三次不同的数即抛弃),第二次验证即可。

JAVA实现如下:

public List<Integer> majorityElement(int[] nums) {
List<Integer> list = new ArrayList<Integer>();
if (nums == null || nums.length == 0)
return list;
int left = nums[0], right=nums[0];
int count1 = 1, count2 = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] == left)
count1++;
else if(right==left){
right=nums[i];
count2=1;
}
else if(right==nums[i])
count2++;
else if(count1==0){
left=nums[i];
count1=1;
}
else if(count2==0){
right=nums[i];
count2=1;
}
else{
count2--;
count1--;
}
}
count1=0;count2=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==left)
count1++;
else if(nums[i]==right)
count2++;
}
if(count1>nums.length/3)
list.add(left);
if(count2>nums.length/3)
list.add(right);
return list;
}

Java for 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. leetcode 229. Majority Element II(多数投票算法)

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

  5. (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 ...

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

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

  7. 【LeetCode】229. Majority Element II

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

  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 OJ 229. Majority Element II

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

随机推荐

  1. 移动端网站优化指南-WAP篇

    转载:http://seofangfa.com/mobile-seo/mobile-seo-guide.html 1.域名优化:启用短域名,例如:m.abc.com,便于用户记忆,方便搜索蜘蛛查找,减 ...

  2. 【Bootstrap】Bootstrap和Java分页-第一篇

    目录 关于此文 pagination BetweenIndex DefaultPagination QueryHandler BookDaoImpl BookServiceImpl BookActio ...

  3. 条件查询N多的情况下,回显解决方法。

    条件查询每个web程序员一定都写过,关于条件回显值页面的思路很简单,将页面的值传到后台,放置request作用域,然后回显至页面. 如果几个条件还好些,如果是下面这种情况呢? 如果条件像以上情况N多情 ...

  4. javascript 时间代理

    <button class="btn-active">按钮1</button> <button>按钮2</button> <b ...

  5. cf251.2.C (构造题的技巧)

    C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...

  6. 编译CDH Spark源代码

    如何编译CDH Spark源代码 经过漫长的编译过程(我编译了2个半小时),最终成功了,在assembly/target/scala-2.10目录下面有spark-assembly-1.0.0-cdh ...

  7. linux之samba与linux权限

    当linux的文件夹或文件为用户或root时,则在window上共享出来的samba是不能进行修改的,当samba设置为 [share]path = /home#available = yes#bro ...

  8. centos7 静态ip设置

    TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV ...

  9. native2ascii.exe

    native2ascii.exe 是 Java 的一个文件转码工具,是将特殊各异的内容 转为 用指定的编码标准文体形式统一的表现出来,它通常位于 JDK_home\bin 目录下,安装好 Java S ...

  10. Sqli-LABS通关笔录-16

    这个关卡之前我还使用了一下工具跑,发现居然跑不出来.这就尴尬了.行吧手工试试. payload admin") and If(ascii(substr(database(),1,1))=11 ...