[LeetCode]Majority Element
来源:互联网 发布:志华软件 编辑:程序博客网 时间:2024/05/26 20:22
Question
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.
本题难度Easy。有3种算法分别是:哈希法、排序法、投票法(最巧妙)
【题意】
You may assume that the array is non-empty and the majority element always exist in the array.
1、哈希法
【复杂度】
时间 O(N) 空间 O(N)
【思路】
在遍历数组的过程中,用一个哈希表记录每个数出现过的次数,如果该次数大于一半,则说明是众数。
【代码】
public class Solution { public int majorityElement(int[] nums) { //require int size=nums.length; Map<Integer,Integer> map=new HashMap<>(); //invariant for(int n:nums){ if(map.containsKey(n)){ if(map.get(n)==size/2)return n; map.put(n,map.get(n)+1); }else map.put(n,1); } //ensure return nums[0];//这是针对只有1个元素情况 }}
2、排序法
【复杂度】
时间 O(NlogN) 空间 O(1)
【思路】
将数组排序,这时候数组最中间的数肯定是众数。
【代码】
public class Solution { public int majorityElement(int[] nums) { //require Arrays.sort(nums); //ensure return nums[nums.length/2]; }}
3、投票法
【复杂度】
时间 O(N) 空间 O(1)
【思路】
设一个投票变量candidate
,还有一个计数变量cnt
,开始遍历数组。如果新数和candidate
一样,那么cnt
加上1;否则,如果cnt==1
,则将candidate
更新为这个新的数,如果cnt>1
,则cnt
减去1。因为每一对不一样的数都会互相消去,最后留下来的candidate
就是众数。
【代码】
public class Solution { public int majorityElement(int[] nums) { //require int size=nums.length; int candidate=nums[0],cnt=1; //invariant for(int i=1;i<size;i++){ int n=nums[i]; if(n==candidate)cnt++; else if(cnt==1)candidate=n; else cnt--; } //ensure return candidate; }}
参考
[Leetcode] Majority Element 众数
- 【leetcode】Majority Element II,Majority element algorithm
- Leetcode: Majority Element &Majority Element II
- Leetcode Majority Element
- leetcode 169-Majority Element
- 【leetcode 哈希表】Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- leetcode: Majority Element
- [LeetCode] Majority Element
- leetcode 153: Majority Element
- leetcode Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- [LeetCode] Majority Element
- Leetcode-Majority Element
- [leetcode]Majority Element
- [LeetCode]Majority Element
- Leetcode: Majority Element
- Java实现银行卡号的校验j
- Kafka官方文档翻译(二)快速入门
- intellij IDEA 设置默认的maven配置
- 代码中的有效注释
- win7下怎样设置putty免用户名密码登陆
- [LeetCode]Majority Element
- 4-4-Reverse Linked List
- 提高 ASP.NET Web 应用性能的 24 种方法和技巧
- OkHttpUtils一个专注于让网络请求更简单的框架
- Linux配置IP
- 8086汇编 模块化程序设计-批量数据的传递
- Android高级之十二讲之内存、电量、卡顿、流量
- PCB设计上如何避免EMC问题
- 利用sys._getframe()为日志添加文件路劲,函数名和行号