题目

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

For example,
Given [3,2,1,5,6,4] and k = 2, return 5.

Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.

思路一:利用排序

看到这个题目,一般想到的方法就是将数组进行排序,然后找出第k大的元素即可。但是,时间复杂度为O(n*logn)

int cmp(const void* a,const void* b){return  (*((int *)a))-(*((int *)b));
}int findKthLargest(int* nums, int numsSize, int k) {if(nums==NULL||numsSize<1||k<0||k>numsSize){return 0;}qsort(nums,numsSize,sizeof(nums[0]),cmp);return nums[numsSize-k];
}

思路二:不完全排序

思路一是全部将数组进行了排序然后选择出结果。其实,我们可以选择不完全排序来解决问题,在快排中,将数组分成两半,前面一半数组中的数全部小于 主元的值,后面一半数组中的数全部大于主元的值,如果主元的索引index

void swap(int *a,int *b){int temp=*a;*a=*b;*b=temp;
}
int partition(int* nums, int start,int end){if(end<start){return -1;}int val=nums[start];int index=start;for(int i=start+1;i<=end;i++){if(nums[i]<val){index++;swap(&nums[index],&nums[i]);}}swap(&nums[start],&nums[index]);return index;
}void sortHelper(int* nums, int start,int end,int numsSize,int k){if(end<=start){return;}int index=partition(nums,start,end);if(index==numsSize-k){return;}else if(index<numsSize-k){sortHelper(nums,index+1,end,numsSize,k);}else if(index>numsSize-k){sortHelper(nums,start,index-1,numsSize,k);}
}int findKthLargest(int* nums, int numsSize, int k) { if(nums==NULL||numsSize<1||k<0||k>numsSize){return 0;} int start=0;int end=numsSize-1;sortHelper(nums,start,end,numsSize,k);return nums[numsSize-k];
}

通过上面的理论分析,理论上第二种方法的运行时间会比第一种的运行时间少,出于好奇的看了下这两种方法的运行时间,如下:

第一种方法的运行时间:

第二种方法的运行时间:

从两张图中可以看出,居然是第一种的还要快。比较郁闷,居然和理论结果不一样。
郁闷的同时也开始找原因,开始以为是因为函数调用开销太大,导致第二种方法的运行时间过慢,因此,就把swap(int *a,int *b) 的功能全部用三行代码在函数partition中实现。居然,根本没有改善。呜呜。

《leetCode》:Kth Largest Element in an Array相关推荐

  1. 力扣215:数组中的第K个最大元素 (leetcode 215:Kth Largest Element In An Array)

    题目链接:https://leetcode.cn/problems/kth-largest-element-in-an-array 目录: 一.题目描述 1.中文 2.英文 二.解决方法 1.直接排序 ...

  2. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array...

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  3. 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  4. LeetCode——Kth Largest Element in an Array

    LeetCode--Kth Largest Element in an Array Question Find the kth largest element in an unsorted array ...

  5. Leetcode: Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  6. [swift] LeetCode 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. 49.Kth Largest Element in an Array

    Level:   Medium 题目描述: Find the kth largest element in an unsorted array. Note that it is the kth lar ...

  8. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  9. [leedcode 215] Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

最新文章

  1. 时间计时android程序,Android 时间计时器控件 Chronometer
  2. 加工中心宏程序生成器_零件行外球面加工,老师傅告诉你,普通程序与宏加工哪个更方便...
  3. 用java调用oracle存储过程总结
  4. JNDI学习总结(1)——JNDI入门简介
  5. db2数据库连接数 linux_linux db2 连接数据库
  6. 2 文件处理、权限管理、搜索
  7. UML类图中会涉及到的一些概念、关系
  8. python中参数的顺序和sys.argv顺序要一样么_Python中 sys.argv[]的用法简明解释
  9. 阿里云张建锋:工业互联网不是工业自动化
  10. kafka消费者如何读同一生产者消息_Kafka系列3:深入理解Kafka消费者
  11. UG NX10.0 软件安装教程
  12. CS领域论文数据分析
  13. vue 检测ie版本_vue判断当前浏览器为IE低版本,给出升级提示;IE11及其他浏览器正常使用-Go语言中文社区...
  14. 麒麟系统编译网卡驱动
  15. 【C#实现猜数字游戏】
  16. oneDrive登陆界面空白 的解决办法
  17. 修复百度编辑器插入视频的bug,可实时预览视频,可修改到支持手机查看视频...
  18. Web防火墙(WAF)是什么?和传统防火墙区别是什么?
  19. API接口测试及常用de接口测试工具
  20. 采用蒙特卡罗方法求解π值

热门文章

  1. 【BOOST C++ 10 时间数据】(4)时间格式输入和输出(11-12)
  2. ThingsBoard安装
  3. re sub 实现多处替换
  4. 图像采集卡 | 以每秒千兆像素的速度进行图像处理
  5. 计算机仿真氢光谱实验,氢氘灯光谱 实验报告
  6. 数字人民币将如何改变金融生态?
  7. 问题 C: 货币系统
  8. openssl s_server s_client 相关命令参数
  9. 论计算机网络信息安全隐患及防御措施,计算机网络安全隐患及其防御措施
  10. PL/SQL编程:过程函数触发器题目分析