class Solution {
public int majorityElement(int[] nums) {
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
int n = nums.length;
for(int num : nums) //统计每个元素出现的次数
{
Integer count = map.get(num);
if(count == null)
count =1;
else
count++;
map.put(num,count);
if(map.get(num) > n/2)
return num;
}
return 0;
}
}
用暴力方法超时了
展开