这题用到的基本算法是Boyer–Moore majority vote algorithm

wiki里有示例代码

 1 import java.util.*;
2 public class MajorityVote {
3 public int majorityElement(int[] num) {
4 int n = num.length;
5 int candidate = num[0], counter = 0;
6 for (int i : num) {
7 if (counter == 0) {
8 candidate = i;
9 counter = 1;
10 } else {
11 if (i == candidate) {
12 counter++;
13 } else {
14 counter--;
15 }
16 }
17 }
18
19 counter = 0;
20 for (int i : num) {
21 if (i == candidate) counter++;
22 }
23 if (counter < (n + 1) / 2) return -1;
24 return candidate;
25
26 }
27 public static void main(String[] args) {
28 MajorityVote s = new MajorityVote();
29 System.out.format("%d\n", s.majorityElement(new int[] {1, 2, 3}));
30 System.out.format("%d\n", s.majorityElement(new int[] {2, 2, 3}));
31 }
32 }

  基本想法是这样的:在数组中数目超过 n /2的元素至多有一个,所以遍历过程中只有一个候选元素

我们假设有某个元素满足这种要求:若他均匀分布,至少每间隔一个出现一次;而且还不够,至少在某处多出现了一次

现在想一下不均匀的情况:如果该元素间隔了很长没出现,则至少在这个长间隔的前面或后面出现密集区域。

vector<int> majorityElement(vector<int>& nums) {
if(nums.empty())
return vector<int>();
if(nums.size() == 1){
return vector<int>({nums[0]});
}
vector<pair<int, int>> candidates;
for(auto num : nums){
if(candidates.size() < 1){
candidates.emplace_back(num, 2);
}
else if(candidates.size() < 2){
if(num != candidates[0].first)
candidates.emplace_back(num, 2);
else
candidates[0].second++;
}
else{
if(num == candidates[0].first){
candidates[0].second++;
candidates[1].second--;
}
else if(num == candidates[1].first){
candidates[1].second++;
candidates[0].second--;
}
else{
candidates[0].second--;
candidates[1].second--;
int ind = candidates[0].second < candidates[1].second ? 0 : 1;
if(candidates[ind].second <= 0){
candidates[ind].first = num;
candidates[ind].second = 2;
}
}
}
}
for(auto& candidate : candidates){
candidate.second = 0;
}
for(auto num : nums){
for(auto& candidate : candidates){
if(num == candidate.first)
candidate.second++;
}
}
vector<int> result;
for(auto candidate:candidates){
if(candidate.second > nums.size() / 3)
result.push_back(candidate.first);
}
return result;
}

  

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 (众数之二)

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  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. fushioncharts的使用教程

    FusionCharts 是使用javascript 实现统计图表的js组件:其官网地址:http://www.fusioncharts.com.其早期版本FusionCharts Free 是基于f ...

  2. 一些不认识的开源js(更新ing。。。)

    孟星魂和小蝶归隐山林曾经说过,我们不问江湖事,但是不能不知道江湖事,因为我们是老伯的人(大概意思),所以有些东西可以用不到,但是一定要了解点... (首先不能人云亦云,但是有个主观观点也没啥大问题) ...

  3. 补第二周四人小组WBS/NABCD

    四人小组项目<东北师范大学论坛> 要求: 1.给出需求概述.功能列表.痛点或亮点.NABCD及WBS模型在此项目中的应用. 2.不熟悉的名词,自行搜索资料并参考教材第393页开始的术语索引 ...

  4. Linux服务器下用svn创建多个项目

    (1): 创建svn仓库路径        mkdir  -p  /opt/svn/project1        mkdir -p   /opt/svn/project2        svnadm ...

  5. javaSE之如何将一个文档显示出来(,txt,.doc,.....)

    package DEMO ; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impo ...

  6. 读书笔记1: 资源地址—通用资源的标识符(URI)

    例子: https://msdn.microsoft.com/zh-cn/library/system.uri(v=vs.110).aspx 解释:协议://主机[:端口号]/绝对路径[参数] 对应的 ...

  7. 用XmlSerializer进行xml反序列化的时候,程序报错: 不应有 <xml xmlns=''>

    原因 一,类型错误: 比如xml本来是UserInfo类型 用XmlSerializer进行反序列化传入的类型是MemberInfo这就会报错 二,xml根节点和对象的类名不一致,而又没有对类加入[X ...

  8. 关于SQL语句优化的一个问题

    今天写了一个很简单的存储过程,结果一执行,40多秒,后来调整了一句话写法,瞬间出来,其实差别不大,如下: select item_no=vpc.ITEM_ID ,BL_QTY=sum(vpc.QTY_ ...

  9. chrome 模拟点击

    实现进入一个页面后触发一个<a>的点击事件. 由于safari和chrome不支持<a>的click()所以需要对浏览器进行判断 var Sys = {};   var ua ...

  10. Objective-C:Block

    Block是OC中一种与其它语言的语法区别较大的一种用法,需要注意: Block也叫代码段,它封装了一段代码,可以在任何时候执行: Block可以作为函数参数或者函数的返回值,而其本身又可以带输入参数 ...