Lintcode: Median
Given a unsorted array with integers, find the median of it. A median is the middle number of the array after it is sorted. If there are even numbers in the array, return the N/2-th number after sorted. Example
Given [4, 5, 1, 2, 3], return 3 Given [7, 9, 4, 5], return 5 Challenge
O(n) time.
Quicksort的变形,quickselect,跟Kth largest Element很像
public class Solution {
/**
* @param nums: A list of integers.
* @return: An integer denotes the middle number of the array.
*/
public int median(int[] nums) {
// write your code here
int len = nums.length;
if (len%2 == 0) return search(nums, len/2, 0, len-1);
else return search(nums, len/2+1, 0, len-1);
}
public int search(int[] nums, int k, int start, int end) {
int l=start, r=end;
int pivot = r;
while (true) {
while (nums[l] < nums[pivot] && l<r) {
l++;
}
while (nums[r] >= nums[pivot] && l<r) {
r--;
}
if (l == r) break;
swap(nums, l, r);
}
swap(nums, l, end);
if (k == l+1) return nums[l];
else if (k > l+1) return search(nums, k, l+1, end);
else return search(nums, k, start, l-1);
}
public void swap(int[] nums, int l, int r) {
int temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
}
}
Lintcode: Median的更多相关文章
- [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- LintCode: Median of two Sorted Arrays
求第k个值 1.归并排序 归并到第k个值为止 时间复杂度:O(k) class Solution { public: // merge-sort to find K-th value double h ...
- LintCode "Sliding Window Median" & "Data Stream Median"
Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...
- lintcode 1: Data Stream Median
Data Stream Median Numbers keep coming, return the median of numbers at every time a new number adde ...
- [LintCode笔记了解一下]80.Median
Given a unsorted array with integers, find the median of it. A median is the middle number of the ar ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- HackerRank "Median Updates"
Same as LintCode "Sliding Window Median", but requires more care on details - no trailing ...
- [OJ] Data Stream Median (Hard)
LintCode 81. Data Stream Median (Hard) 思路: 用一个大根堆保存较小的一半数, 一个小根堆保存较大的一半数. 每次根据num和两个堆顶的数据决定往哪个堆里面放. ...
随机推荐
- PHP学习(四)---PHP与数据库MySql
主要有以下的内容: 1.怎么连接数据库 2.怎么操作数据库 (1)怎么执行sql语言 (2)怎么处理返回的结果集 方法一:面向过程(已经过时,只是了解) 假设: $username=your_name ...
- Why Apache Beam? A data Artisans perspective
https://cloud.google.com/dataflow/blog/dataflow-beam-and-spark-comparison https://github.com/apache/ ...
- fenshijin
#include<stdio.h> int map[6][4]={8,0,18,10, 13,10,15,20, 10,50,13,30, ...
- synchronized的使用方法
[转自] http://blog.csdn.net/witsmakemen/article/details/6966116 记下来,很重要. Java语言的关键字,当它用来修饰一个方法或者一个代码块的 ...
- (转)freemakeer初入门
在web开发过程中,尤其是后台管理系统的开发中,少不了增删改成的基础操作,原来我自己的做法是一份一份的拷贝粘贴,然后修改其中的不同,然而这样既枯燥无味又浪费了大量的时间,所以根据自己项目结构的特点写了 ...
- AMQP协议
当前各种应用大量使用异步消息模型,并随之产生众多消息中间件产品及协议,标准的不一致使应用与中间件之间的耦合限制产品的选择,并增加维护成本. AMQP是一个提供统一消息服务的应用层标准协议,基于此协议的 ...
- SQL Server存储机制二
http://blog.csdn.net/ltylove2007/article/details/21084585 http://www.cnblogs.com/anding/p/3254674.ht ...
- Java学习-029-JSON 之三 -- 模仿 cssSelector 封装读取 JSON 数据方法
前文简单介绍了如何通过 json-20141113.jar 提供的功能获取 JSON 的数据,敬请参阅:Java学习-028-JSON 之二 -- 数据读取. 了解学习过 JQuery 的朋友都知道, ...
- Oracle利用external table 查看trace文件
1. 用下面的语句找到trace文件的路径 select * from v$diag_info where name='Default Trace File'; 2. 创建一个directory用来加 ...
- 微信公众平台开发(110) 微信连Wi-Fi
关键字:微信公众平台 微信连Wi-Fi 微信 WiFi 硬件鉴权作者:方倍工作室 原文:http://www.cnblogs.com/txw1958/p/weixin-wifi.html 微信连Wi- ...