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. DAOFactory复用代码

    工厂设计模式 public class DaoFactory { private static final DaoFactory factory = new DaoFactory(); private ...

  2. jmeter按比例执行业务场景

    可用函数 __counter实现: 函数助手中 找到 __counter,如 ${__counter(false,num)},功能简介 ---- 参数为true,每个用户有自己的计数器 ---- 参数 ...

  3. nodejs 初次链接 mongodb 的详细细节

    时间  2016-06-2613:05:16 在前端的学习也有一段时间了,学习了html,css,javascript,jqery,ajax,php,mysql,学习了这些,了解了一些皮毛,也没有什么 ...

  4. MyBatis框架(三)动态SQL,分页,二进制存入数据库图片

    一.动态sql语句,分页 1, <if>条件 <if test="key!=null"> 拼接sql语句 </if> 2, <choose ...

  5. [js高手之路] html5 canvas系列教程 - 文本样式(strokeText,fillText,measureText,textAlign,textBaseline)

    接着上文线条样式[js高手之路] html5 canvas系列教程 - 线条样式(lineWidth,lineCap,lineJoin,setLineDash)继续. canvas提供两种输出文本的方 ...

  6. TCP/IP(四)网络层

    前言 前面给大家介绍了计算机网络的基本概述,物理层和数据链路层.这一篇给大家介绍面试中经常会被问到的网络层.在介绍之前我们回顾一下之前学习的知识! CP/IP协议栈:物理层.链路层.网络层.传输层.应 ...

  7. Nexus 私有仓库搭建与 Maven 集成

    Nexus 私有仓库搭建与 Maven 集成 |作者:RexFang |出处:http://www.cnblogs.com/rexfang/ |关于作者:Java 程序员一枚 |版权:本文版权归作者和 ...

  8. Two Sum IV - Input is a BST

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  9. JavaWeb(三)JSP概述

    一.JSP概述 1.1.JSP简介 一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%>结束.JSP是一种Java servlet,主要用于实现 ...

  10. Redis “瘦身”指南

    code[class*="language-"], pre[class*="language-"] { background-color: #fdfdfd; - ...