[Problem solution] The Kth largest element in the array

Find the k-th largest element in the unsorted array. Please note that what you need to find is the k-th largest element after sorting the array, not the k-th different element.

示例 1:
输入: [3,2,1,5,6,4] 和 k = 2
输出: 5
示例 2:
输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
输出: 4

Problem solving ideas
1. Sort the array first
2. Return the maximum value of the specified position directly


Lizou Compiler

static int comp(const void* a, const void* b){
    
    
    return *(int*)a - *(int*)b;
}
int findKthLargest(int* nums, int numsSize, int k){
    
    
    //判断数组数据合法性
    if(nums == NULL) return 0;
    //将数组进行排序
    qsort(nums, numsSize, sizeof(int), comp);
    //返回指定的最大元素
    return nums[numsSize - k];
}

This question is used for sorting functions in the C library, which is a bit too speculative. Update later when writing.
Also, the article does not finish updating (cocococo~) forced to lower the standard. Hahaha


Update progress this month

Creation is not easy, your likes are my biggest motivation! ! !
See you next time end~

Guess you like

Origin blog.csdn.net/weixin_43776724/article/details/107028686