169多数元素
给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。
来源:力扣(LeetCode)
int majorityElement(int* nums, int numsSize){
getmaj(nums, 0, numsSize-1);
}
int getmaj(int *nums, int low,int high)
{
int left,right;//记录两个边的众数
int i;
int countleft = 0, countright = 0;
if(low = high)//数组中只有一个数
{
return nums[low];
}
else
{
mid = (low + high)/2;
left = getmaj(nums, low, mid);
right = getmaj(nums, mid+1, high);
if(left = right)//两边众数相等
{
return left;
}
else{
for(i = 0; i <= high;