3 - Two Pointers Algorithm
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的更多相关文章
- 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 ...
- 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 ...
- algorithm之改变序列算法--待解决
简述:改变序列算法,参见http://www.cplusplus.com/reference/algorithm/?kw=algorithm 待解决问题:iterator_traits.std::mo ...
- 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 ...
- nomasp 博客导读:Android、UWP、Algorithm、Lisp(找工作中……
Profile Introduction to Blog 您能看到这篇博客导读是我的荣幸.本博客会持续更新.感谢您的支持.欢迎您的关注与留言.博客有多个专栏,各自是关于 Android应用开发 .Wi ...
- 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 ...
- [Algorithm] 如何正确撸<算法导论>CLRS
其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话 ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
随机推荐
- 使用nginx做反向代理和负载均衡效果图
连续请求nginx服务器,响应如图: 参考文章:https://segmentfault.com/a/1190000007495181
- svn 安装
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...
- PHP多维数组替换某一元素的值
数组结构如下所示: $arr = [ [ 'id' => 1, 'sub'=> [ [ 'value' => 11.2 ], [ 'value' => 34.5 ] ] ], ...
- Vue与React的异同
众所周知,前端现在最火的两个框架是Vue和React了.通过一段时间的学习与项目上的实践,我想通过比较他们之间的异同点来发现以后在项目的技术选型中知道怎么抉择用哪个.有一点说明的是他们各自有自己的优势 ...
- 自己绘制table,加分页
- 在sql中case子句的两种形式
case子句,在select后面可以进行逻辑判断. 两种形式:判断相等.判断不等 一.判断相等的语法: case 列名 when ... then ... when ... then ... el ...
- laravel composer
composer config -g repo.packagist composer https://packagist.phpcomposer.com 改安装包的全局镜像网址
- JQuery选择器,动画,事件和DOM操作
JQuery是由JS封装的一些方法,供我们调用,可以快速的实现某些JS功能,实际是JS编写的方法包 将JQuery文件放到JS文件夹下,然后引用到<head></head>中 ...
- Microsoft SQL Server 【Windows 身份验证】和 【sa】都无法登录的解决方案
1.修改启动参数:打开[SQL Server 配置管理器(SQL Server Configuration Manager)]→右键[SQL Server(MSSQLSERVER)]属性→高级(Adv ...
- XPosed 示例
https://blog.csdn.net/fmc088/article/details/80535894