力扣-169. 多数元素

int cmp(const void *testA, const void *testB)
{
    int* a = (int*)testA;/*强制转换类型*/
	int* b = (int*)testB;
	return *a - *b;/*如果降序排列那就是*b - *a*/
}
int majorityElement(int* nums, int numsSize)
{
    int pos = 0;
    qsort(nums, numsSize, sizeof(int), cmp);/*对数组进行升序排列*/
    pos = (numsSize - 1) / 2;
    return nums[pos];
}

你可能感兴趣的:(LeetCode,leetcode,c语言)