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 ...
随机推荐
- IBM服务器安装Ubuntu Linux server 64以及网络配置
最近在部署AC环境,云AC要求软件环境为Ubuntu 14.04 版本的服务器Linux操作系统,下面是环境部署的准备工作: 一.下载文件 (1)下载系统文件 地址:http://mirrors.16 ...
- 接口自动化框架(java)--2.接口用例POST请求,参数配置
这套框架的报告是自己封装的 Post类型的接口通常有请求参数,请求参数也是json类型,所以需要写一个类将请求参数序列化成json对象 以常见的登录接口为例 新建一个package,和postPara ...
- vue组件弹窗
定义弹窗组件 先写一个普通的vue组件,其显示的内容就是弹窗的内容. 文件的位置 /src/views/toast/toast.vue <template> <div class=& ...
- git之commit
面解释的话, 1.git commit -m用于提交暂存区的文件: 2.git commit -am用于提交跟踪过的文件. 要理解它们的区别,首先要明白git的文件状态变化周期,如下图所示 工作目录下 ...
- 【数据结构】算法 LinkList (Insertion Sort List 链表插入排序)
将一个单链表进行处理后,所得结果为一有序链表 Solution: 将原始链表逐个查询,插入新链表,在插入的同时对链表进行排序.时间复杂度O(n*n) public ListNode insertion ...
- kaggle learn python
def has_lucky_number(nums): return any([num % 7 == 0 for num in nums]) def menu_is_boring(meals): &q ...
- Angular4中使用后台去数据,Swiper不能滑动的解决方法(一)
Swiper是目前较为流行的移动端触摸滑动插件,因为其简单好用易上手,很受很多设计师的欢迎. 今天在使用Swiper的时候遇到这个问题: 使用angularjs动态循环生成swiper-slide ...
- web接口文档apidoc的使用
1.安装 npm install apidoc -g 2.新建src文件夹,里面放2个文件,test.js和apidoc.json 3.test.js /** * @api {get} /query_ ...
- 利用 postMessage 进行数据传递 (iframe 及web worker)及问题
一 postMessage应用于主页面和iframe之间进行数据的传递 1 子iframe页面向主页面进行数据传递: // 多个子iframe需要将自己的计数统计到主页面进行数据上报 window. ...
- CSC 172 (Data Structures and Algorithms)
Project #3 (STREET MAPPING)CSC 172 (Data Structures and Algorithms), Spring 2019,University of Roche ...