主元素问题 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 ...
随机推荐
- eclipse在注释时候字体变成繁体字
输入法和java中的快捷键冲突了,按下ctrl+shift+F就切换回去了
- 批处理no.bat
在公司每次我启动电脑, 网络连接需要一段时间, 而我想在这段小时间里面, 一旦网络连接成功就帮我启动微信和qq, 如果还没有连接成功就继续监测直到有网络了才会成功才会打开两个程序, 当打开程序后脚本自 ...
- kali meterpreter中mimikatz模块获取密码
kali这方面不说了, meterpreter也略过, 做个关于mimikatz的笔记. mimikatz模块, 能获取对方机器的密码(包括哈希和明文). 渗透模块怎么进的也不说了, 方式太多, 我用 ...
- Kali系列之multi/handler(渗透win7)
环境 靶机 192.168.137.133 kali 192.168.137.135 步骤+ 生成后门 msfvenom -p windows/meterpreter/reverse_tcp LHOS ...
- 【4opencv】求解向量和轮廓的交点
在“学习OpenCV3"的QQ群众,网友且行且珍惜针对前期博客(https://www.cnblogs.com/jsxyhelu/p/9345590.html)中的内容提出了以下问题: 比如 ...
- linux tmux 工具使用 tmux.conf 文件
set -g prefix ^a unbind ^b bind a send-prefix unbind '"' bind - splitw -v unbind % bind \ split ...
- [内核驱动] DOS路径转化为NT路径
转载:http://blog.csdn.net/qq_33504040/article/details/78468278 最近在做一个文件过滤驱动程序,禁止访问指定目录或文件.想要从R3给R0发命令和 ...
- Python3 tkinter基础 Listbox Button 点击按钮删除选中的单个元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 字体在win10下显示模糊,有锯齿
目录 系统设置: 修改冲突软件启动设置: vscode: vs2017: atom: gpu软件修改: 参考: 系统设置: 一般为了提高性能,会关闭平滑屏幕字体边缘 修改冲突软件启动设置: 经过 gp ...
- jvm:垃圾收集器
垃圾收集器: Serial 收集器: 单线程收集器,专注做收集,会暂停别的工作.收集效果好. ParNew 收集器: 是Serial的多线程版本.目前只有它能和CMS收集器配合. Paralle ...