LeetCode215:数组中的第K个最大元素 (C、C++实现)

题目

LeetCode215.数组中的第K个最大元素
https://leetcode-cn.com/problems/kth-largest-element-in-an-array/
在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
示例 1:
输入: [3,2,1,5,6,4] 和 k = 2
输出: 5
示例 2:
输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
输出: 4

题解

堆实现

C++实现

//调用priority_queue(最小堆)
创建:priority_queue<int, vector, greater> minHeap; //第一个参数是存储对象的类型,第二个参数是存储元素的底层容器,第三个参数是函数对象,它定义了一个用来决定元素顺序的断言。

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int, vector<int>, greater<int>> minHeap; 
        for (auto n : nums) {  //使用auto
            if(minHeap.size()==k){
                if(n > minHeap.top()){
                    minHeap.pop();
                    minHeap.push(n);
                }
            }else{
                minHeap.push(n);
            } 
        }
        return minHeap.top();
    }
};

int main(int argc, const char * argv[]) {
    vector<int> nums ={3,2,1,5,6,4};
    Solution test;
    int k = test.findKthLargest(nums, 2);
    cout<<k<<endl;
    return 0;
}

C实现

int findKthLargest(int* nums, int numsSize, int k) {
    int sort[k];
    for (int i = 0,j=0; i<numsSize; i++) {
        if(j==0){
            sort[j]= nums[i];
            j++;
        }else{
            bool flag = false;
            for (int n=0; n<j ; n++) {
                if (nums[i] > sort[n]) {
                    int low;
                    if(j==k) low = j-1;
                    else low =j;
                    for (; low>n; low--) {
                        sort[low]=sort[low-1];
                    }
                    sort[n] = nums[i];
                    if(j<k) j++;
                    flag = true;
                    break;
                }
            }
            if(j<k && !flag){
                sort[j] = nums[i];
                j++;
            }
        }
    }
    return sort[k-1];
}

int main(int argc, const char * argv[]) {
    int nums[6] = {3,2,1,5,6,4};
    int k = findKthLargest(nums, 6, 2);
    printf("%d",k);
    return 0;
}

2、用最小堆实现

#include <stdio.h>
#include <stdlib.h>

void heapSwap(int *a,int*b){
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

void heapSort(int *heap, int heapSize) {
    int dad = 1, son = dad << 1;
    while (son <= heapSize) {
        if (son+1 <= heapSize && heap[son+1] < heap[son]) {
            son++;
        }
        if (heap[dad] > heap[son]) {
            heapSwap(&heap[dad], &heap[son]);
            dad = son;
            son = dad << 1;
        }
        else {
            return;
        }
    }
}
void heapInsert(int* heap,  int *heapSize,int num) {
    heap[++(*heapSize)] = num;
    int son = *heapSize, dad = son >> 1;
    while (dad) {
        if (heap[son] < heap[dad]) {
            heapSwap(&heap[son], &heap[dad]);
            son = dad;
            dad = son >> 1;
        }
        else {
            return;
        }
    }
}

int findKthLargest(int* nums, int numsSize, int k) {
    int *heap = malloc(sizeof(int *)*(k+1)); //分配堆空间
    int heapsize = 0;
    for (int i=0;i<numsSize;i++){
        if (heapsize == k) { //堆满情况
            if(nums[i] > heap[1]){ //比堆顶最大元素大时进行插入
                heap[1] = nums[i];
                heapSort(heap,heapsize);
            }
        }else { //堆未满直接插入
            heapInsert(heap,&heapsize,nums[i]);
        }
    }
    return heap[1];
}

int main(int argc, const char * argv[]) {
    int nums[6] = {3,2,1,5,6,4};
    int k = findKthLargest(nums, 6, 2);
    printf("%d",k);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值