LeeCode-Majority Element

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(int* nums, int numsSize)
{
if(numsSize==)
return nums[]; int i,j,k;
for(i=;i<numsSize;i++)
{
for(j=i-;j>=;j--)
{
if(nums[i]>=nums[j])
break;
} if(i!=j-)
{
int temp=nums[i];
for(k=i-;k>j;k--)
{
nums[k+]=nums[k];
}
nums[k+]=temp;
}
} int Time;
Time=numsSize/;
int count=;
for(i=;i<numsSize-;i++)
{
if(nums[i]==nums[i+])
{
count++;
if(count>Time)
{
return nums[i];
}
}
else
{
count=;
}
} return ;
}
上一篇:Ant Design 4.0 正式版发布:暗色主题、组件重做


下一篇:创建用于自定义SharePoint解决方案部署的Visual Studio项目