【数组】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.
思路:
首先,我们来看一下怎样求众数,也就是元素出现大于⌊ n/2 ⌋的数。
我们注意到这样一个现象: 在任何数组中,出现次数大于该数组长度一半的值只能有一个。 通过数学知识,我们可以证明它的正确性,但是这并不在我们这篇博客里涉及。摩尔投票法的基本思想很简单,在每一轮投票过程中,从数组中找出一对不同的元素,将其从数组中删除。这样不断的删除直到无法再进行投票,如果数组为空,则没有任何元素出现的次数超过该数组长度的一半。如果只存在一种元素,那么这个元素则可能为目标元素。那么有没有可能出现最后有两种或两种以上元素呢?根据定义,这是不可能的,因为如果出现这种情况,则代表我们可以继续一轮投票。因此,最终只能是剩下零个或一个元素。在算法执行过程中,我们使用常量空间实时记录一个候选元素c以及其出现次数f(c),c即为当前阶段出现次数超过半数的元素。根据这样的定义,我们也可以将摩尔投票法看作是一种动态规划算法。
接着,我们来看我们今天要解决的问题。
这道题让我们求出现次数大于⌊ n/3 ⌋的众数,而且限定了时间和空间复杂度,那么就不能排序,也不能使用哈希表,这么苛刻的限制条件只有一种方法能解了,那就是摩尔投票法 Moore Voting,这种方法在之前那道题Majority Element 求众数中也使用了。题目中给了一条很重要的提示,让我们先考虑可能会有多少个众数,经过举了很多例子分析得出,任意一个数组出现次数大于n/3的众数最多有两个,具体的证明我就不会了,我也不是数学专业的。那么有了这个信息,我们使用投票法的核心是找出两个候选众数进行投票,需要两遍遍历,第一遍历找出两个候选众数,第二遍遍历重新投票验证这两个候选众数是否为众数即可,选候选众数方法和前面求众数一样,由于之前那题题目中限定了一定会有众数存在,故而省略了验证候选众数的步骤,这道题却没有这种限定,即满足要求的众数可能不存在,所以要有验证。
/**
* @param {number[]} nums
* @return {number[]}
*/
var majorityElement = function(nums) {
var a,b,countA=0,countB=0;
for(var i=0,len=nums.length;i<len;i++){
if(nums[i]==a){
countA++;
}else if(nums[i]==b){
countB++;
}else if(countA==0){
countA=1;
a=nums[i];
}else if(countB==0){
countB=1;
b=nums[i]
}else{
countA--;
countB--;
}
} countA=0;
countB=0;
for(var i=0,len=nums.length;i<len;i++){
if(nums[i]==a){
countA++;
}
if(nums[i]==b){
countB++;
}
} var res=[];
if(countA>Math.floor(len/3)){
res.push(a);
}
if(countB>Math.floor(len/3)){
res.push(b);
} return res; };
【数组】Majority Element II的更多相关文章
- LeetCode(169)Majority Element and Majority Element II
一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...
- Majority Element,Majority Element II
一:Majority Element Given an array of size n, find the majority element. The majority element is the ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- Majority Element(169) && Majority Element II(229)
寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...
- 【刷题-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 ...
- LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III
LeetCode169. Majority Element Given an array of size n, find the majority element. The majority elem ...
- 229. Majority Element II -- 找出数组中出现次数超过 ⌊ n/3 ⌋ 次的数
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
随机推荐
- (水题)987654321 problem -- SGU 107
链接: http://vj.acmclub.cn/contest/view.action?cid=168#problem/G 时限:250MS 内存:4096KB 64位IO格式:%I ...
- A Good Story for Great Friends
There once was a little girl who had a bad temper. Her mother gave her a bag of nails and told her t ...
- git archive命令详解
git archive可以将加了tag的某个版本打包提取出来,例如: git archive -v --format= > v0..zip --format表示打包的格式,如zip,-v表示对应 ...
- asp.net Mvc 模型绑定项目过多会导致页面运行时间卡
asp.net Mvc 模型绑定项目过多会导致页面运行时间卡的问题. 解决方式就是采用ModelView方式进行精简,已减少模型绑定及验证的时间.
- redis 任务队列
使用Redis实现任务队列 说到队列很自然就能想到Redis的列表类型,3.4.2节介绍了使用LPUSH和RPOP命令实现队列的概念.如果要实现任务队列,只需要让生产者将任务使用LPUSH命令加入到某 ...
- Jersey服务端
问世间情为何物,直叫人一声呵呵. 上个项目写的jersey restful服务端,怎么都是正确的,没什么问题.结果这个项目写了,呵呵了,真的呵呵了,怎么搞都有问题. 总是报错,对json的类型报错,无 ...
- 在Azure DevOps Server (TFS) 中修改团队项目名称
概述 [团队项目]: 在Azure DevOps Server (原名TFS)中,团队项目(Team Project)是一个最基本的数据组织容器,包含了一个团队或者信息系统中的所有信息,包括源代码.文 ...
- json--pyton中obj与json的互转,js中obj与json的互转
json 解释:json是一种跨平台的通用的数据格式 python中对象(obj)与json之间的相互转换 1.对象(obj)转json格式的字符串 json.dumps(res) res = () ...
- Day 1. 占位符的使用方法(%d,%s)(格式化输出)
方法1 name = input("请输入名字1") age = input ("请输入年龄") hobby = input("请输入兴趣爱好&qu ...
- 使用session的监听器获取当前在线人数
1首先在web.xml中配置Session的监听器 2创建监听器并且继承HttpSessionListener 3.在jsp中导入监听器 4.获取当前在线人数 5.配置到公共网络(使用natapp的免 ...