636.Kth Largest Element in an Array

1.Problem

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.

题意很简单,找到一个一维数组中的第K大的数并返回。数组中第K大的数也是面试中经常考察的问题。现在就借Leetcode上的这题来详细总结下这个问题的几种解法。

2.Solution

//排序 时间复杂度为O(N*logN) 空间复杂度O(1)
class Solution {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k ];
}
}
//优先队列来维护数据的有序性,超出k的部分移除出优先队列 时间复杂度O(N*logK),空间复杂度O(K)
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> p = new PriorityQueue<>();
for ( int a : nums ) {
p.offer(a); if ( p.size() > k ) {
p.poll();
}
}
return p.peek();
}
}
//快速排序思想,平均时间复杂度O(N),最坏的情况下会是O(N^2),空间复杂度为O(1)
class Solution {
public int findKthLargest(int[] nums, int k) {
int Left = 0,Right = nums.length - 1;
while (Left < Right ) {
int left = Left;
int right = Right;
int key = nums[left]; while ( left < right ) {
while ( left < right && nums[right] < key ) {
right--;
} nums[left] = nums[right]; while ( left < right && nums[left] >= key ) {
left++;
} nums[right] = nums[left];
} nums[left] = key;
if ( left == k - 1 ) {
return nums[left];
} else if ( left > k - 1 ) {
Right = left - 1;
} else {
Left = left + 1;
}
}
return nums[k - 1];
}
}

Leetcode 之 Kth Largest Element in an Array的更多相关文章

  1. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

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

  2. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

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

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

  4. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...

  5. [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字

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

  6. 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. Java for 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 ...

  8. 【leetcode】Kth Largest Element in an Array (middle)☆

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

  9. [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素

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

随机推荐

  1. Java - web.xml文件中可以配置哪些内容?

    web.xml用于配置Web应用的相关信息,如:监听器(listener).过滤器(filter).Servlet.相关参数.会话超时时间.安全验证方式.错误页面等,下面是一些开发中常见的配置: ①配 ...

  2. 数据库设计(三)11 important database designing rules which I follow

    原文地址:https://www.codeproject.com/Articles/359654/important-database-designing-rules-which-I-fo 麻辣个

  3. VM下redhat9.0不能上网

    近期本人在学习linux时,安装Red Hat Linux9后,可是上不了网,弄得查资料还得切换到虚拟机上去,特耗时间.还好没有疯掉! 首先,测试下你的linux看是否是这类问题,输入ping www ...

  4. Problem b 莫比乌斯反演+枚举除法的取值

    莫比乌斯反演+枚举除法的取值 第二种形式: f(n)表示gcd(x,y)=n的数量. F(n)表示gcd(x,y)是n的倍数的数量. /** 题目:Problem b 链接:https://vjudg ...

  5. 线程同步工具CountDownLatch

    CountDownLatch的一个非常典型的应用场景是:有一个任务想要往下执行,但必须要等到其他的任务执行完毕后才可以继续往下执行.假如我们这个想要继续往下执行的任务调用一个CountDownLatc ...

  6. 说明反转控制(IOC)和面向方向编程(AOP)在spring中的应用

    说明反转控制(IOC)和面向方向编程(AOP)在spring中的应用 解答:Spring 核心容器(Core)提供Spring框架的基本功能.核心容器的主要组件是BeanFactory,它是工厂模式的 ...

  7. linux程序设计——主机字节序和网络字节序(第十五章)

    15.2.10    主机字节序和网络字节序 当在基于intel处理器的linux机器上执行新版本号的server和客户程序时,能够用netstat命令查看网络连接状况.它显示了客户/server连接 ...

  8. CI和CD的意思

    openstack中CI和CD的意思: 持续集成(CI)和持续交付(CD)

  9. Ubuntu14.4下搭配WEB服务器(apache + php + mysql)

    今天,趁着自己动手安装web服务器的余热,将Ubuntu14.4搭配WEB服务器的过程记录下来. “一切皆文件”. 说明:网上关于类似搭配web服务器的教程,案例不计其数,但自己亲自动手“试试”,一定 ...

  10. el表达式的首字母大小写问题

    EL表达式获取对象属性的原理是这样的: 以表达式${user.name}为例 EL表达式会根据name去User类里寻找这个name的get方法,此时会自动把name首字母大写并加上get前缀,一旦找 ...