数组中出现次数超过一半的数字
数组中出现次数超过一半的数字
http://www.nowcoder.com/questionTerminal/e8a1b01a2df14cb2b228b30ee6a92163
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
if(numbers.size()==0)
{
return 0;
}
int sizes=numbers.size();
sort(numbers.begin(),numbers.end());//讲数组进行递增排序
int i,count=0,middle=numbers[numbers.size()/2];//取排序后数组的中间元素
for(i=0;i<sizes;i++) //只有数组中存在符合题意得元素,那数组中间元素肯定是这个元素
{
if(numbers[i]==middle)
{
count++;
}
}
return (count>numbers.size()/2)? middle:0;
}
};</int>