[Leetcode] Majority Element 众数
Majority Element I
Given an array of size n, find the majority element. The majority element is the element that appears more than
⌊ n/2 ⌋
times.You may assume that the array is non-empty and the majority element always exist in the array.
哈希表法
复杂度
时间 O(N) 空间 O(N)
思路
在遍历数组的过程中,用一个哈希表记录每个数出现过的次数,如果该次数大于一半,则说明是众数。
排序法
复杂度
时间 O(NlogN) 空间 O(1)
思路
将数组排序,这时候数组最中间的数肯定是众数。
代码
public class Solution {public int majorityElement(int[] nums) {Arrays.sort(nums);return nums[nums.length / 2];}
}
位操作法
复杂度
时间 O(N) 空间 O(1)
思路
假设一个数是最多只有32位的二进制数,那么我们从第一位到第32位,对每一位都计算所有数字在这一位上1的个数,如果这一位1的个数大于一半,说明众数的这一位是1,如果小于一半,说明大多数的这一位是0。
投票法
复杂度
时间 O(N) 空间 O(1)
思路
记录一个candidate变量,还有一个counter变量,开始遍历数组。如果新数和candidate一样,那么counter加上1,否则的话,如果counter不是0,则counter减去1,如果counter已经是0,则将candidate更新为这个新的数。因为每一对不一样的数都会互相消去,最后留下来的candidate就是众数。
代码
public class Solution {public int majorityElement(int[] nums) {int candidate = nums[0], cnt = 0;for(int i = 1; i < nums.length; i++){if(candidate == nums[i]){cnt++;} else if(cnt==0){candidate = nums[i];} else {cnt--;}}return candidate;}
}
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.
投票法
复杂度
时间 O(N) 空间 O(1)
思路
上一题中,超过一半的数只可能有一个,所以我们只要投票出一个数就行了。而这题中,超过n/3的数最多可能有两个,所以我们要记录出现最多的两个数。同样的两个candidate和对应的两个counter,如果遍历时,某个候选数和到当前数相等,则给相应计数器加1。如果两个计数器都不为0,则两个计数器都被抵消掉1。如果某个计数器为0了,则将当前数替换相应的候选数,并将计数器初始化为1。最后我们还要遍历一遍数组,确定这两个出现最多的数,是否都是众数。
代码
public class Solution {public List<Integer> majorityElement(int[] nums) {List<Integer> res = new ArrayList<Integer>();if(nums.length == 0) return res;int c1 = 1, c2 = 0, n1 = nums[0], n2 = 0;for(int i = 1; i < nums.length; i++){// 如果和某个候选数相等,将其计数器加1if(nums[i] == n1){c1++;} else if(nums[i] == n2){c2++;// 如果都不相等,而且计数器都不为0,则计数器都减1} else if(c1 != 0 && c2 != 0){c1--;c2--;// 如果某个计数器为0,则更新相应的候选数} else {if(c1 == 0){n1 = nums[i];c1 = 1;} else {n2 = nums[i];c2 = 1;}}}c1 = 0;c2 = 0;for(int i = 0; i < nums.length; i++){if(nums[i] == n1) c1++;else if(nums[i] == n2) c2++;}if(c1 > nums.length / 3) res.add(n1);if(c2 > nums.length / 3) res.add(n2);return res;}
}
[Leetcode] Majority Element 众数相关推荐
- [LeetCode]Majority Element
题目描述: Given an array of size n, find the majority element. The majority element is the element that ...
- LeetCode Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- LeetCode Majority Element II(Moore Voting Algorithm即Majority Voting Algorithm)
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algo ...
- LeetCode - Majority Element
参考了别人的想法,思路简洁,效率高.虽然是在充分理解别人的思路后写出的代码,但是还是发现了自己的不足之处.以后还是要多思考多写.加油吧! public class Solution {public i ...
- [leetcode]Majority Element II
思路:可以使用三向切分的快速排序算法,因为这种算法会把小于某个数,等于某个数,和大于某个数的分开,分成三部分.很容易判断等于某个数的全部数量,对于这种重复值问题,很有效. public class S ...
- C#LeetCode刷题之#169-求众数(Majority Element)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4048 访问. 给定一个大小为 n 的数组,找到其中的众数.众数是 ...
- 【LeetCode】169. Majority Element
原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...
最新文章
- redis3.2安装过程分享
- oracle单表数据量上亿_MySQL数据库中,数据量越来越大,有什么具体的优化方案么?...
- python3入门与进阶笔记_我的Python3萌新入门笔记
- 【科技金融】专家评分卡和机器学习评分卡的差异化和必要性
- Verilog MIPS32 CPU(一)-- PC寄存器
- 嵌入式linux tftp服务的配置
- 远控免杀专题8---BackDoor-Facktory免杀
- 虚拟主机选择php版本,虚拟主机的php版本如何选择
- 记录:注意Unity返回的所有数组,只是数组的副本
- 劝你们,千千千万不要当一个程序员!!!!!!
- python语言程序设计教程答案赵璐_python语言程序设计教程课后答案赵璐
- 一个人的精神结构和他的精神资源
- 几何光学类毕业论文文献有哪些?
- Win11系统电脑硬盘分区方法教程
- [音视频] wav 格式
- 第二章 关系模型和关系运算理论 3类完整性
- Outlook邮箱如何在手机上登录
- 漫步数理统计十四——重要的不等式
- 本地Web项目写好后,要怎么样才能Post到WWW互联网里面去?
- 微信小程序支付功能用服务器吗,微信小程序 支付功能 服务器端(TP5.1)实现...
热门文章
- 20160828_第4周周报
- 设一组初始记录关键字序列为(25,50,15,35,80,85,20,40,36,70)进行一趟归并后的结果为
- C语言typedef与#define的区别
- C++ array初始化需要双层大括号
- MYSQL错误代码#1045 Access denied for user 'root'@'localhost'
- 【Leetcode | 03】String
- 想学IT的必看!今年Android面试必问的这些技术面,架构师必备技能
- Odoo免费开源企业信息化平台助力企业成功
- bzoj1222: [HNOI2001]产品加工
- macaca web(4)