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. javaWeb中servlet开发——监听器

    监听的定义 对application的监听 application是servletContext接口的对象,表示的是整个上下文环境,如果要想实现对application监听则可以使用如下两个接口: s ...

  2. improve performance whilemaintaining the functionality of a simpler and more abstract model design of processor hardware

    Computer Systems A Programmer's Perspective Second Edition In this chapter, we take a brief look at ...

  3. ubuntu日志清理

    由于ubuntu日志文件syslog 和 kern.log 时刻在增长,一会儿就使得根目录文件夹不够用了,需使用如下命令清理 sudo -i输入密码echo  > /var/log/syslog ...

  4. Linux的权限说明

    Linux的权限不是很细致,只有RWX三种r(Read,读取):对文件而言,具有读取文件内容的权限:对目录来说,具有浏览目录的权限.w(Write,写入):对文件而言,具有新增,修改,删除文件内容的权 ...

  5. UIView 的粗浅解析

    The UIView class defines a rectangular area on the screen and the interfaces for managing the conten ...

  6. FW:使用weave管理docker网络

    Posted on 2014-11-12 22:20 feisky 阅读(1761) 评论(0) 编辑 收藏 weave简介 Weave creates a virtual network that ...

  7. FW docker使用问题总结,解决国内不能访问gcr.io的问题

    docker使用问题总结 解决国内不能访问gcr.io的问题 国内可以通过https://dashboard.daocloud.io来下载. 比如?gcr.io/google_containers/p ...

  8. java中计算两个日期之间天数的程序设计。

    //用java编写出一个以下方法计算两个日期之间天数的程序设计. import java.util.regex.Matcher; import java.util.regex.Pattern; pub ...

  9. UML时序图总结

    前言 在我的工作中,用的最多的就是时序图了.可能由于工作的原因,我也是最喜欢画时序图了,很清楚,很明了,什么时候发送什么消息,到达什么状态,一下子就展示在你的脑海里,对于消息驱动的程序来说,是再好不过 ...

  10. MongoDB聚合查询

    1.count:查询记录条数 db.user.count() 它也跟find一样可以有条件的 db.user.count({}) 2.distinct:用来找出给定键的所有不同的值 db.user.d ...