Code & Func
2017-11-10

第45天。

今天的题目是Majority Element:

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.

最简单的想法就是,遍历一遍序列,记录出现的次数,然后在遍历一遍刚才记录的次数,如果大于k就直接返回,这种时候一般会用到hash table,在c++中就是unordered_map:

1
int majorityElement(vector<int>& nums) {
2
int size = nums.size();
3
int k = size/2;
4
unordered_map<int,int> map;
5
for(auto i:nums) map[i]++;
6
for(auto p:map) if (p.second > k) return p.first;
7
}

这种方法的时间复杂度是O(n),空间复杂度也是O(n).

虽然很简单,但是这道题目在dicuss中也有很多有趣的解法:

  • 因为Majority Element在序列中存在n/2个,所以假如这个序列时有序的话,他一定会出现在中间:
1
int majorityElement(vector<int>& nums) {
2
nth_element(nums.begin(), nums.begin() + nums.size() / 2, nums.end());
3
return nums[nums.size() / 2];
4
}

nice的学到了一个新的函数。

  • 同样是因为出现了k/2次,所以我们如果随机选取一个元素的话,有一半的概率可以直接选到Majority Element:
1
int majorityElement(vector<int>& nums) {
2
int n = nums.size();
3
srand(unsigned(time(NULL)));
4
while (true) {
5
int idx = rand() % n;
6
int candidate = nums[idx];
7
int counts = 0;
8
for (int i = 0; i < n; i++)
9
if (nums[i] == candidate)
10
counts++;
11
if (counts > n / 2) return candidate;
12
}
13
}
  • Moore Voting Algorithm,这个方法的正确性我也不是很确定:
1
int majorityElement(vector<int>& nums) {
2
int major, counts = 0, n = nums.size();
3
for (int i = 0; i < n; i++) {
4
if (!counts) {
5
major = nums[i];
6
counts = 1;
7
}
8
else counts += (nums[i] == major) ? 1 : -1;
9
}
10
return major;
11
}
上一条动态