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的更多相关文章

  1. [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 ...

  2. LintCode: Median of two Sorted Arrays

    求第k个值 1.归并排序 归并到第k个值为止 时间复杂度:O(k) class Solution { public: // merge-sort to find K-th value double h ...

  3. LintCode "Sliding Window Median" & "Data Stream Median"

    Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...

  4. lintcode 1: Data Stream Median

    Data Stream Median Numbers keep coming, return the median of numbers at every time a new number adde ...

  5. [LintCode笔记了解一下]80.Median

    Given a unsorted array with integers, find the median of it. A median is the middle number of the ar ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  8. HackerRank "Median Updates"

    Same as LintCode "Sliding Window Median", but requires more care on details - no trailing ...

  9. [OJ] Data Stream Median (Hard)

    LintCode 81. Data Stream Median (Hard) 思路: 用一个大根堆保存较小的一半数, 一个小根堆保存较大的一半数. 每次根据num和两个堆顶的数据决定往哪个堆里面放. ...

随机推荐

  1. zabbix 2.2.2在centos 6.3 x86_64上的安装

    zabbix 2.2.2在centos 6.3 x86_64上的安装   更新五月 03, 2014     # 依赖环境 yum install -y php-mbstring mysql-deve ...

  2. jQuery之换肤与cookie插件

    有时候一个网页可以有多个皮肤进行选择,也就是不同的背景,或是一整套新的css,能使整个页面变成另一种风格. 这个功能可以用jQuery来实现.外加cookie插件.有了cookie,就可以长时间的保存 ...

  3. Python - KMP算法

    def kmp_match(tex, pat): n = len(tex) m = len(pat) tex = '0' + tex pat = '0' + pat pi = [] pi.append ...

  4. Ubuntu/Deepin下常用软件汇总(持续更新)

    最近开始用Ubuntu了,好多软件都不是常用的了,在这边留底,以免忘记.如果没有写安装方法,则直接在软件源中可以找到 UNetbootin U盘制作工具,制作Ubuntu的安装U盘超好用 Braser ...

  5. BadgeView的使用介绍

    在现在大部分的信息发布类应用,都有这样的一个功能:当后台数据更新,比如有系统消息或者是用户间有互动的时候,通过在控件上显示一个小红点来提示用户有新的信息.一般来说,这种业务需求,我们可以在布局文件中隐 ...

  6. filter:Alpha总结

    filter:Alpha(Opacity=?, FinishOpacity=?, Style=?, StartX=?, StartY=?, FinishX=?, FinishY=?)Opacity:透 ...

  7. Cygwin + CMake 测试

    https://www.cygwin.com/ apt-get for cygwin? wget rawgit.com/transcode-open/apt-cyg/master/apt-cyg in ...

  8. 20145211 《Java程序设计》第10周学习总结——昨夜星辰昨夜风

    教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴. 在发送和接收 ...

  9. House Building---hdu5538(求表面积水题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5538 题意:有一个三维的图形,先给出平面图是n*m的矩形,每个位置都有不同个数的方块,a[i][j]代 ...

  10. A Guide to Creating a Quality Project Schedule

    Successful projects start with a good quality project schedule. Creating a schedule is one of the fi ...