《leetCode》:Majority Element
タイトル
構想
まず並べ替えて、真ん中を取ればいいです.もっと多くのアイデアはこのブログを見ることができます.http://blog.csdn.net/u010412719/article/details/49047033
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.
構想
まず並べ替えて、真ん中を取ればいいです.もっと多くのアイデアはこのブログを見ることができます.http://blog.csdn.net/u010412719/article/details/49047033
int cmp(const void *a,const void *b){
return ((*(int*)a)-(*(int*)b));
}
int majorityElement(int* nums, int numsSize) {
if(nums==NULL||numsSize<1){
return 0;
}
qsort(nums,numsSize,sizeof(nums[0]),cmp);
return nums[numsSize/2];
}