题目链接:
题目:
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.
Example 1:
Input: [3,2,3] Output: 3
Example 2:
Input: [2,2,1,1,1,2,2] Output: 2
题目分析:
给定一个长度为 n 的数组,求出其中的最主要的数(众数)。这里众数的定义是:数量超过 ⌊
n/2⌋
。
解题思路:
根据题目分析,假设有3个数,那么众数数量至少为2;假设有4个数,那么众数数量至少为3。也就是说,无论如何,众数的数量肯定会超过一半的。
那么一开始的想法,将数组进行排序,然后 nums[n/2],最中间的数就是众数。因为数量一定超过一般,所以无论在最前面,最后面还是中间,nums[n/2] 对应的数一定就是众数。
这个的时间复杂度是 O(nlogn)。
那么接下来想一想有没有时间更短的方法,比如说时间复杂度仅仅只有 O(n) 之类的。
有一个算法称为 Moore's voting algorithm :
基本思想是:每次都找出一对不同的元素,从数组中删掉,直到数组为空或只有一种元素。 不难证明,如果存在元素 e 出现频率超过半数,那么数组中最后剩下的就只有e。刚好用于解决这道题。
即我们先将计数为 1,并指向第一个数,接下来遍历数组,如果 nums[i] 和指向记录的数相等,计数加1 ;否则计数减1。最后如果计数器为 0时,说明已经出现几组不同的两个数使得计数为0,这样子我们就重新将此时的数 记录下来,并计数器变为1。最后就输出记录下的数,就是众数。
这就利用了 Moore's voting algorithm。计数器计算出现相同的数,如果不同的话,就一起删除两个不同的数。这样子最后记录的数一定是众数,因为众数数量超过一半。
这种方法只用遍历一次数组,所以时间复杂度是 O(n)。
AC代码:
第一种利用排序的,O(nlogn):
class Solution {
public:
int majorityElement(vector<int>& nums) {
int th = nums.size()/2;
sort(nums.begin(),nums.end());
return nums[th];
}
};
第二种Moore's voting algorithm,O(n):
class Solution {
public:
int majorityElement(vector<int>& nums) {
int cnt = 1;
int res = nums[0];
for(int i = 1;i < nums.size();i++)
{
if(cnt == 0)
{
cnt = 1;
res = nums[i];
}
else
{
if(nums[i] == res)
cnt++;
else
cnt--;
}
}
return res;
}
};