主元素问题 Majority Element
2018-09-23 13:25:40
主元素问题是一个非常经典的问题,一般来说,主元素问题指的是数组中元素个数大于一半的数字,显然这个问题可以通过遍历计数解决,时间复杂度为O(n),空间复杂度为O(n)。这样的算法有两个弊端,一是空间复杂度较高,二是没法处理数据流问题。
因此就有了Boyer-Moore Majority Vote algorithm,这个算法可以用来高效的解决主元素问题,并且空间复杂度降到了O(1),时间复杂度保持不变。
算法的思路就是将不同的元素进行抵消,最后剩余的就是最终的结果。
如果说题目中没有明确说明一定存在主元素,那么还需要额外一次遍历来确认当前的解为主元素。
一、主元素问题
问题描述:

问题求解:
public int majorityElement(int[] nums) {
int candidate = 0;
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == candidate) count++;
else if (count == 0) {
candidate = nums[i];
count = 1;
}
else count--;
}
return candidate;
}
二、Follow Up
问题描述:

问题求解:
public List<Integer> majorityElement(int[] nums) {
if (nums == null || nums.length == 0) return new ArrayList<>();
int candidate1 = 0;
int candidate2 = 0;
int count1 = 0;
int count2 = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == candidate1) count1++;
else if (nums[i] == candidate2) count2++;
else if (count1 == 0) {
candidate1 = nums[i];
count1 = 1;
}
else if (count2 == 0) {
candidate2 = nums[i];
count2 = 1;
}
else {
count1--;
count2--;
}
}
List<Integer> res = new ArrayList<>();
count1 = 0;
count2 = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == candidate1) count1++;
else if (nums[i] == candidate2) count2++;
}
if (count1 > nums.length / 3) res.add(candidate1);
if (count2 > nums.length / 3) res.add(candidate2);
return res;
}
主元素问题 Majority Element的更多相关文章
- LeetCode OJ 229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【算法31】寻找数组的主元素(Majority Element)
题外话 最近有些网友来信问我博客怎么不更新了,是不是不刷题了,真是惭愧啊,题还是在刷的,不过刷题的频率没以前高了,看完<算法导论>后感觉网上很多讨论的题目其实在导论中都已经有非常好的算法以 ...
- Majority Element:主元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode OJ:Majority Element II(主元素II)
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- lintcode 中等题:majority number III主元素III
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O( ...
- lintcode 中等题:Majority number II 主元素 II
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时 ...
- 【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)
题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the ma ...
- majority element(数组中找出出现次数最多的元素)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- leetcode——169 Majority Element(数组中出现次数过半的元素)
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- 关于actor模型
actor model是1973年就提出的一个分布式并发编程模型,在erlang语言中得到广泛支持和应用.目前Java中也出现了很多支持actor模型的库:akka.killim.jetlang等等, ...
- 【MonkeyRunner环境搭建】
一.配置MonkeyRunner环境变量 1.首先下载一个AndroidSDK,在SDK的目录中的tools文件夹中,直接带有MonkeyRunner 2.打开MonkeyRunner的方式: |-- ...
- Python3基础 list enumerate 将列表的每个元素转换成 带索引值的元组
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 弄懂linux shell对包含$的变量的执行过程?
参考: http://www.linuxidc.com/Linux/2012-04/58095.htm 在包含变量的命令中, 命令是怎么执行的呢? 首先, 它会原封不动的, 只是按原样替换变量的内容. ...
- Logstash Introduction
https://www.cnblogs.com/aresxin/p/8035137.html Elasticsearch是个开源分布式搜索引擎,提供搜集.分析.存储数据三大功能.它的特点有:分布式,零 ...
- 如何自己烧制全文RSS(打造自己RSS源)
烧制RSS源 到Feed43注册一个账号,虽说不注册也能用,但是为了方便修改自己烧制的RSS,最好还是注册一个账号来管理 到主页点击Create new feed 输入网址点击reload 可以看到请 ...
- 【注册码】Matlab7.0(R14)注册码
Matlab 7 (R14) 注册码1:14-13299-56369-16360-32789-51027-35530-39910-50517-56079-43171-43696-14148-64597 ...
- C++中的string常用函数用法
标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而 ...
- Ubuntu yindaoxiufu 引导修复(Boot Repair)
Ubuntu yindaoxiufu 引导修复(Boot Repair) from: http://blog.csdn.net/piaocoder/article/details/50589667 ...
- 案例:8,64,256都是2的阶次方数(8是2的3次方),用Java编写程序来判断一个整数是不是2的阶次方数。
如果一个数是2的阶次方数,则它的二进制数的首位一般是1,后面全为0.比如8:1000,64:1000000,如果将这个数减1后再作与&运算,则应该全为0,(x&(x-1)==0&am ...