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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
这里要隆重介绍一下O(n)复杂度的 Boyer–Moore majority vote algorithm。这是一个专门统计序列中出现频率最高的字符的算法,伪代码如下:
Initialize an element m and a counter i with i = 0
For each element x of the input sequence:
If i = 0, then assign m = x and i = 1
else if m = x, then assign i = i + 1
else assign i = i − 1
Return m
转换成这道题 java 的解法就是:
public class Solution {
public int majorityElement(int[] nums) {
int i = 0, count = 0, majority = 0;
while (i < nums.length) {
if (count == 0) {
majority = nums[i];
count ++;
} else if (majority == nums[i]) {
count ++;
} else {
count --;
}
i++;
}
return majority;
}
}
实在是巧妙。其实这道题还有很多其他的解法,比如sorting, hashmap, moore voting, bit manipulation。具体的代码实现如下:
// Sorting
public int majorityElement1(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
// Hashtable
public int majorityElement2(int[] nums) {
Map<Integer, Integer> myMap = new HashMap<Integer, Integer>();
//Hashtable<Integer, Integer> myMap = new Hashtable<Integer, Integer>();
int ret=0;
for (int num: nums) {
if (!myMap.containsKey(num))
myMap.put(num, 1);
else
myMap.put(num, myMap.get(num)+1);
if (myMap.get(num)>nums.length/2) {
ret = num;
break;
}
}
return ret;
}
// Moore voting algorithm
public int majorityElement3(int[] nums) {
int count=0, ret = 0;
for (int num: nums) {
if (count==0)
ret = num;
if (num!=ret)
count--;
else
count++;
}
return ret;
}
// Bit manipulation
public int majorityElement(int[] nums) {
int[] bit = new int[32];
for (int num: nums)
for (int i=0; i<32; i++)
if ((num>>(31-i) & 1) == 1)
bit[i]++;
int ret=0;
for (int i=0; i<32; i++) {
bit[i]=bit[i]>nums.length/2?1:0;
ret += bit[i]*(1<<(31-i));
}
return ret;
}
最后一个 Bit manipulation 非常巧妙,因为序列中的数都是 int 型(32位)。分别统计序列中每一个数每一位出现的次数,大于n/2的位即属于出现次数大于n/2的那个数,把属于的位合并,得到最终结果,思想是Divide and Conquer。