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. java自然语言理解demo,源码分享(基于欧拉蜜)

    汇率换算自然语言理解功能JAVA DEMO >>>>>>>>>>>>>>>>>>>&g ...

  2. 转自知乎(JAVA后台开发可以纯粹用JAVA SE吗?)

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:巴多崽链接:http://www.zhihu.com/question/29663744/answer/45154839来源: ...

  3. UI自动化测试(二)浏览器操作及对元素的定位方法(xpath定位和css定位详解)

    Selenium下的Webdriver工具支持FireFox(geckodriver). IE(InternetExplorerDriver).Chrome(ChromeDriver). Opera( ...

  4. struts2的防止表单重复提交

    防止表单重复提交其实就是struts2的一个拦截器的使用: struts.xml配置文件: <?xml version="1.0" encoding="UTF-8& ...

  5. Codeforces Round #410 (Div. 2)C. Mike and gcd problem

    题目连接:http://codeforces.com/contest/798/problem/C C. Mike and gcd problem time limit per test 2 secon ...

  6. R语言包下载(转载)

    http://blog.csdn.net/hongjinlongno1/article/details/53130893 包含几乎所有包,很方便

  7. 【机器学习实战】第11章 使用 Apriori 算法进行关联分析

    第 11 章 使用 Apriori 算法进行关联分析 关联分析 关联分析是一种在大规模数据集中寻找有趣关系的任务. 这些关系可以有两种形式: 频繁项集(frequent item sets): 经常出 ...

  8. Flash与 Javascript 交互

    网页加载时立即调用 ExternalInterface.addCallback中定义的函数会失败,放到按键中调用正常. 推测:可能是flash对象加载时间略长,网页加载到js时,flash对象尚未初始 ...

  9. vs 或 Sql server2012连接Sql server时出现的问题:已成功与服务器建立连接,但在登陆过程中发生错误

    以前连接是正常的,就这两天连不上了.(没有耐心的直接看末尾解决办法) 错误消息如下: 1.尝试读取或写入受保护的内存.这通常指示其他内存已损坏.(System.Data) 2.已成功与服务器建立连接, ...

  10. Sublime Text3使用指南

    前言(Prologue) Sublime Text是一款跨平台代码编辑器(Code Editor),从最初的Sublime Text 1.0,到现在的Sublime Text 3.0,Sublime ...