标签:leetcode
@author:wepon
@blog:http://blog.csdn.net/u012162613
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.
int majorityElement(vector<int> &num) { unordered_map<int,int> map; int len=num.size(); for(int i=0;i<len;i++){ if(map.find(num[i])==map.end()) map[num[i]]=0; else map[num[i]]++; if(map[num[i]]>=len/2) return num[i]; } }
【leetcode 哈希表】Majority Element
标签:leetcode
原文地址:http://blog.csdn.net/u012162613/article/details/42080621