题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
if(numbers.empty()) return 0;
sort(numbers.begin(),numbers.end());
int middle = numbers[numbers.size()/2];
int count = 0;
for(int i = 0; i < numbers.size(); i++){
if(numbers[i] == middle) count++;
}
return (count > numbers.size()/2) ? middle : 0;
}
};