您的位置:首页 > 其它

[LeetCode]Majority Element

2016-12-09 17:40 232 查看
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