LeetCode 229. Majority Element II (众数之二)
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 (众数之二)的更多相关文章
- [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 ...
- leetcode 229 Majority Element II
这题用到的基本算法是Boyer–Moore majority vote algorithm wiki里有示例代码 1 import java.util.*; 2 public class Majori ...
- leetcode 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public: vector<int& ...
- 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 ...
- (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 ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- 【LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 【刷题-LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- 【LeetCode】229. Majority Element II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 hashmap统计次数 摩尔投票法 Moore Vo ...
随机推荐
- 深入理解分布式调度框架TBSchedule及源码分析
简介 由于最近工作比较忙,前前后后花了两个月的时间把TBSchedule的源码翻了个底朝天.关于TBSchedule的使用,网上也有很多参考资料,这里不做过多的阐述.本文着重介绍TBSchedule的 ...
- Servlet 3.0 使用注解配置URl提示404错误
我的环境是 Eclipse oxygen + Servlet 3.0 因为3.0已经开始使用注解了 之前我都是配置listenner 还有Servlet mapping 在 web.xml 中 就 ...
- [转]IOS开发中的CGFloat、CGPoint、CGSize和CGRect
http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGGeometry/Reference ...
- 都是Javascript的作用域惹得祸
案件重现 今天有位然之OA 系统的定制开发用户咨询了个问题,他想在新加的功能模块的操作面板中,实现用户点击删除按钮时提示友好提醒,如下: 问题很简单,虽然他自己最终达到目的效果了,但不知道起初问题出在 ...
- oracle 数据库管理员
一.数据库管理员每个oracle数据库应该至少有一个数据库管理员(dba),对于一个小的数据库,一个dba就够了,但是对于一个大的数据库可能需要多个dba分担不同的管理职责.那么一个数据库管理员的主要 ...
- 简单说明CGI是什么
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Linux之不得不说的init(Linux启动级别的含义 init 0-6)
init 0:关机: init 1:单用户模式(只root进行维护): init 2:多用户 init 3:完全多用户 init 4:安全模式 init 5:图形化 init 6:重启 可以在/etc ...
- socket及其相关(续篇)
IO 多路复用 基本概念 IO多路复用是指内核一旦发现进程指定的一个或者多个IO条件准备读取,它就通知该进程.IO多路复用适用如下场合: (1)当客户处理多个描述字时(一般是交互式输入和网络套接口), ...
- Java历程-初学篇 Day02变量,数据类型和运算符
一,数据类型 1,基础数据类型 整型 byte short int long 浮点型 float double 字符型 char 布尔类型 boolean 2,引用类型 String 字符串型 二,变 ...
- 【转】 文档与笔记利器 reStructuredText 和 Sphinx
关于制作文档和笔记这种事,我已经纠结了很久,网上解决方案也一大推,我试过几样,ScrapBook 和 Zotero,编辑不太方便,同步麻烦.Google Note 过于格式简单,现在也不更新了,Goo ...