思路:使用hashmap实现
public class Solution {
public int majorityElement(int[] nums) {
int len = nums.length,
m = (len % 2 == 0)?len/2:(len/2 + 1);
if(len < 3) {
return nums[0];
}
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int key:nums) {
if(!map.containsKey(key)) {
map.put(key,1);
}else{
int count = map.get(key);
count++;
if(count >= m) {
return key;
}
map.put(key,count);
}
}
return 0;
}
}
本文介绍了一种利用哈希表解决数组中众数问题的算法,通过实例演示了如何实现并优化该算法。
535

被折叠的 条评论
为什么被折叠?



