leetcode面试准备:Kth Largest Element in an Array

1 题目

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.

接口:int findKthLargest(int[] nums, int k)

2 思路

找出数组中第k大的元素。

分治的思想,利用partition的过程结果,第k的数,在排序后的len - k的位置。

3 代码

       /**
* 偷懒的做法
*/
public int findKthLargest0(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k];
} /**
* 分治的思想,利用partition的过程结果,第k的数,在排序后的len - k的位置。
*/
public int findKthLargest(int[] nums, int k) {
int len = nums.length;
int goal = len - k;
int left = 0, right = len - 1;
int index = partition(nums, left, right);
while (index != goal) {
if (index < goal) {
left = index + 1;
} else {
right = right - 1;
}
index = partition(nums, left, right);
}
return nums[goal]; } private int partition(int[] a, int p, int r) {
int i = p - 1;
int x = a[r];
for (int j = p; j < r; j++) {
if (a[j] <= x) {
i++;
swap(a, i, j);
}
}
swap(a, i + 1, r);
return i + 1;
} private void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}

4 总结

分治

leetcode面试准备:Kth Largest Element in an Array的更多相关文章

  1. LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

    题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...

  2. 【LeetCode】215. Kth Largest Element in an Array (2 solutions)

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  3. 【刷题-LeetCode】215. Kth Largest Element in an Array

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  4. 【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 ...

  5. 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...

  6. LeetCode OJ 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. 【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. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

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

  9. Leetcode 之 Kth Largest Element in an Array

    636.Kth Largest Element in an Array 1.Problem Find the kth largest element in an unsorted array. Not ...

随机推荐

  1. 忘记root口令的处理方法

    最近在拜读<Unix & Linux 大学教程>这本书,这本书写得不错.关于Unix和Linux比较介绍比较深入根源,内容也比较详细,很值得去拜读.       在书中的附录中有一 ...

  2. 数据读取器对象SqlDataReader与数据适配器对象SqlDataAdapter的使用

        一.数据读取器对象SqlDataReader的使用      如何执行有查询结果集的select语句. 1.SqlDataReader对象的作用:当包含select语句的SqlCommad对象 ...

  3. HTML5标签及使用方法描述

    HTML 5 作为新一代的超文本标记语言,增加了许多标签.这些标签不但更有语义,而且功能强大.具体有以下标签: <article> 定义外部的内容.比如来自一个外部的新闻提供者的一篇新的文 ...

  4. WIN8+VS2013编写发布WCF之一(编写)

      引言:上学期因为写服务器用WCF,所以连查资料再瞎调试勉强成功了,但是这学期又到了用WCF的时候,而当时的资料零零散散,查找不易,并且此次是在WIN8与VS2013环境下编写的,所以将该入门过程记 ...

  5. OC4_NSString操作

    // // main.m // OC4_NSString操作 // // Created by zhangxueming on 15/6/10. // Copyright (c) 2015年 zhan ...

  6. (转)传统MySQL+ Memcached架构遇到的问题

    实际MySQL是适合进行海量数据存储的,通过Memcached将热点数据加载到cache,加速访问,很多公司都曾经使用过这样的架构,但随着业务数据量的不断增加,和访问量的持续增长,我们遇到了很多问题: ...

  7. (转)TCP注册端口号大全

    分类: 网络与安全 cisco-sccp 2000/tcp Cisco SCCPcisco-sccp 2000/udp Cisco SCCp# Dan Wing <dwing&cisco ...

  8. post 提交数据

    1 默认:application/x-www-form-urlencoded 在网页表单中可设置 enctype的值,如果不设,默认是 application/x-www-form-urlencode ...

  9. Java多线程练习:ticket卖票程序

    /*需求:简单的卖票程序多个窗口买票 */ class Ticket extends Thread{    private static int tick=100;    public void ru ...

  10. vbscript multiple line syntax

    Vbscript 如何将输出内容换行? ' VbCrLf represetns Carriage return–linefeed combination, for more information s ...