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.

解法:参考编程之美129页,寻找发帖水王

代码如下:

public class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer>list=new ArrayList<Integer>();
if(nums==null) return list;
int len=nums.length;
if(len==0) return list;
if(len==1){
list.add(nums[0]);
return list;
}
int m1=nums[0];
int m2=0;
int c1=1;
int c2=0;
for(int i=1;i<len;i++){ if(nums[i]==m1)
c1++;
else if(nums[i]==m2)
c2++;
else if(c1==0){
m1=nums[i];
c1=1;
}else if(c2==0){
m2=nums[i];
c2=1;
}
else{
c1--;
c2--;
}
}
c1=0; c2=0;
for(int i=0;i<len;i++){
if(nums[i]==m1)
c1++;
else if(nums[i]==m2)
c2++;
}
if(m1!=m2){ //防止[0,0]情况出现,其实不加也可以,加了运行时间缩短20ms
if(c1>len/3) list.add(m1);
if(c2>len/3) list.add(m2);
}else{
list.add(m1);
}
return list; }
}

  

 运行结果:

 

(medium)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. Java for 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】229. Majority Element II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...

随机推荐

  1. [Android设计模式]Android退出应用程序终极方法

    如何干净彻底地退出Android应用程序,是很多开发者的心头痒.如何干净地关闭所有已打开的Activity? 如何关闭指定的Activity? 如何关闭一类Activity? 这里,我们提出一种通过实 ...

  2. XAMPP 在windows下无法启动Apache解决方案

    XAMPP 在windows下无法启动Apache解决方案 一.现象 XAMPP 点击Start Apache时出现如下错误 20:41:12  [Apache] Error: Apache shut ...

  3. IOS开发-cell的动态高度

    tableView中自定义cell的高度随子控件的内容动态变化,也是用的非常多的地方.现在就来处理一个自定义一个里面有文字(多少不定),图片(有无不定)的cell 首先要准备两个模型,一个是存放数据的 ...

  4. Neutron分析(2)——neutron-server启动过程分析

    neutron-server启动过程分析 1. /etc/init.d/neutron-server DAEMON=/usr/bin/neutron-server DAEMON_ARGS=" ...

  5. Value Dispose() cannot be called while doing CreateHandle().

    在backgroundWorker run之前show出了一个窗体_frm. _frmpw = new FrmPleaseWait(); _frmpw.SetMsg("正在请求") ...

  6. (转)Lambda表达式详解

    本文转载自:http://www.cnblogs.com/knowledgesea/p/3163725.html 前言 1.天真热,程序员活着不易,星期天,也要顶着火辣辣的太阳,总结这些东西. 2.夸 ...

  7. bzoj4642: 泡泡

    Description "OI真的像是一条奇趣横生的路啊,也许它是绕过了高考的大山,也许确实有通往大学的捷径.但我,真的,真的只在 乎那路上美丽的泡泡." --TB   TB喜欢所 ...

  8. CenOS下搭建VPN服务

    公司生产环境使用的是阿里云主机,采用的是两台nginx主机进行反向代理,现在需要内网一台服务器能够访问公网,所以在nginx服务器上搭建了VPN服务,用于进行内网访问公网. 系统环境:CenOS 6. ...

  9. Add 1G to a LVM on VMware

    1. update disk1 to 5G from 4G in vcenter2. echo 1 > /sys/block/sda/device/rescan3. fdisk /dev/sda ...

  10. Bind9用view配主从

    We use two Bind server to realize view, master, slave----------------------------------------------- ...