您的位置:首页 > 其它

169. Majority Element

2016-12-27 04:50 155 查看
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.
这道题的naive解法是O(n2),两个for循环,挨个统计数字出现的个数,超过n/2就返回,提交完之后第42个测试实例超时了,只能想复杂度更低的算法。
这里要隆重介绍一下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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: