关闭

【Leetcode】Kth Largest Element in an Array

2999人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/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 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.

思路:

1、快排,时间复杂度O(nlogn)

2、分冶..用快排划分,递归的划分。每一次划分都可以去确定一个元素的位置,也就知道比该元素小的元素数目。目测时间复杂度最差为 O(n^2),最好为O(1),平均?没动力做了......待做

算法:

public int findKthLargest(int[] nums, int k) {  
    Arrays.sort(nums);  
    return nums[nums.length-k];  
}  


1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】

【215-Kth Largest Element in an Array(数组中第K大的数)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-28 06:57
  • 4088

【Leetcode 动态规划】 买卖股票 I II III IV 冷却,共5题

买卖股票问题
  • Dr_Unknown
  • Dr_Unknown
  • 2016-07-18 09:26
  • 4510

Leetcode全数组问题

目录 1、编号2 Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respe...
  • wangxiaojun911
  • wangxiaojun911
  • 2014-02-06 03:32
  • 6319

Leetcode--Add to List 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 ...
  • wanna_wsl
  • wanna_wsl
  • 2017-07-03 11:05
  • 135

[leetcode]215. Kth Largest Element in an Array

题意:给一个数组,求出数组中的第K大数,比如[3,2,1,5,6,4] 求出第2大数,则为5 分析:复习堆排序,对于这道题来讲,找第k大数,可以通过一个小顶堆实现。首先取数组中的前k个数组成小顶堆(具...
  • u010057864
  • u010057864
  • 2016-07-26 21:28
  • 251

Leetcode Algorithm 215. Kth Largest Element in an Array

Leetcode Algorithm 215. Kth Largest Element in an Array 给定一个乱序的非空整型数数组,返回数组中第k大的数
  • rinsonlo
  • rinsonlo
  • 2017-03-04 17:14
  • 94

(重点复习)[LeetCode]Kth Largest Element in an Array

QuestionFind the kth largest element in an unsorted array. Note that it is the kth largest element i...
  • byamao1
  • byamao1
  • 2016-12-15 21:40
  • 112

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...
  • u010013453
  • u010013453
  • 2017-03-11 20:31
  • 100

[LeetCode] Kth Largest Element in an Array

Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it...
  • wangshaner1
  • wangshaner1
  • 2015-05-25 20:25
  • 2292

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...
  • Dream_angel_Z
  • Dream_angel_Z
  • 2015-06-13 20:58
  • 1058
    个人资料
    • 访问:555531次
    • 积分:8547
    • 等级:
    • 排名:第2649名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论