5. Kth Largest Element (quick sort的变种)

https://www.lintcode.com/problem/kth-largest-element/description?_from=ladder&&fromId=1

public class Solution {
/**
* @param n: An integer
* @param nums: An array
* @return: the Kth largest element
*/
public int kthLargestElement(int n, int[] nums) {
// write your code here
quickSort(nums, 0, nums.length - 1);
int len = nums.length;
return nums[len - n];
} public void quickSort(int[] nums, int left, int right) {
if(left >= right) {
return;
}
int mid = nums[left + (right - left) / 2];
int l = left, r = right;
while(l <= r) {
while(l <= r && nums[l] < mid) {
l++;
}
while(l <= r && nums[r] > mid) {
r--;
}
if(l <= r) {
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++;
r--;
}
}
quickSort(nums, left, r);
quickSort(nums, l, right);
}
}

3 - Two Pointers Algorithm的更多相关文章

  1. Codeforces Round #539 (Div. 1) 1109F. Sasha and Algorithm of Silence's Sounds LCT+线段树 (two pointers)

    题解请看 Felix-Lee的CSDN博客 写的很好,不过最后不用判断最小值是不是1,因为[i,i]只有一个点,一定满足条件,最小值一定是1. CODE 写完就A,刺激. #include <b ...

  2. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  3. algorithm之改变序列算法--待解决

    简述:改变序列算法,参见http://www.cplusplus.com/reference/algorithm/?kw=algorithm 待解决问题:iterator_traits.std::mo ...

  4. algorithm@ find kth smallest element in two sorted arrays (O(log n time)

    The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directl ...

  5. nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……

    Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸.本博客会持续更新.感谢您的支持.欢迎您的关注与留言.博客有多个专栏,各自是关于 Android应用开发 .Wi ...

  6. The algorithm learning of sort which include Bubblesort,Insertsort,Quicksort and Mergesort.

    Notice : these algorithms achieved by Java. So,let's going to it. firstly, what is Bubblesort? why w ...

  7. [Algorithm] 如何正确撸<算法导论>CLRS

    其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话 ...

  8. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  9. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

随机推荐

  1. Java bean 转 Map

    Java bean 转 Map 时需要使用Fastjson //方法 一 Map<String, Object> a = (Map<String, Object>)JSON.t ...

  2. php爬虫入门

    本篇文章介绍PHP抓取网页内容技术,利用PHP cURL扩展获取网页内容,还可以抓取网页头部,设置cookie,处理302跳转. 一.cURL安装 采用源码安装PHP时,需要在configure时添加 ...

  3. jOrgChart二叉树效果

    引进文件: <link rel="stylesheet" type="text/css" href="Public/com/jQrgChart/ ...

  4. Oracle VM Virtual 安装 ubuntu 后设置全屏

    按照正常流程在vm中安装了ubuntu之后,发现ubuntu系统无法全屏显示,解决途径如下: 1.在vm中点击设置 2.选择“安装增强功能” 3.正常情况下,我们可以在桌面看到一个光盘图标(文件名:V ...

  5. 寻找U2OS中表达的基因及其promoter并用于后续annotation

    方法1.RNA-seq得到不同表达程度基因 方法2. 直接download U2OS_gene.csv https://cancer.sanger.ac.uk/cell_lines/download ...

  6. angular4 使用swiper 首次加载问题(一)

    angular 在使用外部插件swiper 还是有不少小坑的,下面来聊一聊.angular在使用swiper 的一些坑 一开始觉得使用外部引入的方式比较好,就在外部定义了.简单快捷方便, 但是在开发后 ...

  7. Android的JSON数据解析

    一. 使用原生方式解析 准备工作:准备一个布局文件,用来显示元数据与转换之后的数据 <?xml version="1.0" encoding="utf-8" ...

  8. winform Combobox出现System.Data.DataRowView的解决的方法

    个人总结: 1.触发了SelectedIndexChanged事件时:comboBox1.DataSource = dt;要放在comboBox1.SelectedIndex = 0;的上面 comb ...

  9. Jenkins>>>应用篇>>>插件使用>>>Publish over SSH

    依赖环境 SSH: 远程机开启SSH服务.同意Jenkins所在机器通过SSH服务登录到远程机运行脚本. 能够设置SSH使用username/password或通过key登录,SSH配置请查专门的资料 ...

  10. 通过Charles获取看书神器API

    Charles Charles是一个可以做HTTP代理/ HTTP监视器/反向代理的软件,使开发人员能够查看其机器和Internet之间的所有HTTP和SSL / HTTPS流量.包括请求,响应和HT ...