查找数组中出现次数超过一半的数
int majorityElement(vector<int>& nums)
{
map<int,int> map_num;
for(int i=0;i<nums.size()-1;i++)
{
map_num[nums[i]]++;
}
map<int,int>::iterator p;
int max=0;
int element;
while(p!=map.end())
{
if(p->second>max)
{
max=p->second;
element=p->first;
}
}
return element;
}
class Solution {
public:
int majorityElement(vector<int>& nums) {
int size=nums.size()/2;
int element;
while(nums.size()!=0)
{
int count=1;
for(int j=nums.size()-2;j>=0;j--)
{
if(nums[j]==nums[nums.size()-1])
{
nums.erase(nums.begin()+j);
count++;
}
}
if (count>size)
{
element=nums[nums.size()-1];
}
nums.erase(nums.begin()+nums.size()-1);
}
return element;
}
};