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.


题目标签:Array

  题目给了我们一个 nums array, 让我们找出所有 数量超过 n/3 的众数。这一题与 众数之一 的区别在于,众数之一 只要找到一个 众数大于 n/2 的就可以。这一题要找到所有的数量 大于n/3的众数,其实,最多也就会有两个 大于n/3 的众数。因为可能会出现两个众数,而且是要数量 大于n/3 的,所以我们需要遍历两次nums array。

  第一次遍历 需要找出数量出现最多的 两个数字;

  第二次遍历 需要找出准确的出现次数,因为第一次遍历可能会漏掉一些次数;

  最后,要检查次数大于 n/3 的 才算众数。

Java Solution:

Runtime beats 65.76%

完成日期:04/06/2017

关键词:Array

关键点:Moore Voting,需要两次遍历找出 一个或两个 众数

 public class Solution
{
public List<Integer> majorityElement(int[] nums)
{
ArrayList<Integer> res = new ArrayList<>();
int result1 = 0, result2 = 0, count1 = 0, count2 = 0;
// find out two numbers with most occurrence.
for(int i=0; i<nums.length; i++)
{ if(result1 == nums[i])
count1++;
else if(result2 == nums[i])
count2++;
else if(count1 == 0)
{
result1 = nums[i];
count1 = 1;
}
else if(count2 == 0)
{
result2 = nums[i];
count2 = 1;
}
else
{
count1--;
count2--;
}
}
// set counts to 0.
count1 = 0;
count2 = 0;
// count the correct occurrence.
for(int i=0; i<nums.length; i++)
{
if(nums[i] == result1)
count1++;
else if(nums[i] == result2)
count2++;
} // check 1/3 condition.
if(count1 > nums.length / 3)
res.add(result1);
if(count2 > nums.length / 3)
res.add(result2); return res;
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4606822.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

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(多数投票算法)

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

  4. 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 ...

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

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

随机推荐

  1. 磁盘管理之inode与block

    索引式文件系统 什么是inode? Inode其实就是索引号,便于我们寻找我们文件所存储的数据块block,索引式文件系统在查找信息,读写操作上都比原来的文件系统要快,我们可以通过inode中记录的b ...

  2. Spring - Spring容器概念及其初始化过程

    引言 工作4年多,做了3年的java,每个项目都用Spring,但对Spring一直都是知其然而不知其所以然.鄙人深知Spring是一个高深的框架,正好近期脱离加班的苦逼状态,遂决定从Spring的官 ...

  3. 初触hibernate01--MYSQL建表时type=InnoDB和Engine=InnoDB注意点

    第一次运行程序时,将Hibernte中的hibernate.hbm2ddl.auto设置成create(或者是update),让Hibernate帮助自动建表,但不成功,报了如下信息: You hav ...

  4. [js高手之路] html5 canvas系列教程 - 掌握画直线图形的常用API

    我们接着上文[js高手之路] html5 canvase系列教程 - 认识canvas以及基本使用方法继续. 一.直线的绘制 cxt.moveTo( x1, y1 ): 将画笔移动到x1, y1这个点 ...

  5. AngularJS–service(服务)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 服务 Angular的服务也是使用依赖注入(dependency injection ( ...

  6. Digital Square 搜索

    Digital Square Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  7. Doing Homework again

     Doing Homework again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  8. 使用Fabric一键批量部署上线/线上环境监控

    本文讲述如何使用fabric进行批量部署上线的功能 这个功能对于小应用,可以避免开发部署上线的平台,或者使用linux expect开发不优雅的代码. 前提条件: 1.运行fabric脚本的机器和其他 ...

  9. springboot使用zookeeper(curator)实现注册发现与负载均衡

    最简单的实现服务高可用的方法就是集群化,也就是分布式部署,但是分布式部署会带来一些问题.比如: 1.各个实例之间的协同(锁) 2.负载均衡 3.热删除 这里通过一个简单的实例来说明如何解决注册发现和负 ...

  10. 由String的构造方法引申出来的java字符编码

    在String类的constructors中,有一个constructor是将int数组类型转化为字符串: int[] num = {48,49,50,51,52}; String numStr = ...