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和两个堆顶的数据决定往哪个堆里面放. ...
随机推荐
- buffer overflow
Computer Systems A Programmer's Perspective Second Edition We have seen that C does not perform any ...
- common.js
//检测浏览器 function checkb(){ var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua. ...
- AndEngine
AndEngine http://www.oschina.net/question/54100_16765
- java笔记--关于线程同步(5种同步方式)【转】
为何要使用同步? java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数据的增删改查), 将会导致数据不准确,相互之间产生冲突,因此加入同步锁以避免在该线程没有完 ...
- nrf51822-主从通信分析1
建议看该教程前,先看一下 简单扫描器实现 教程 讲解基于sdk目录下central中的两个例子. 关于主机的程序框架其实和从机都是一样的,都是基于事件驱动的框架. Main函数中完成初始化, 从机 ...
- 大数据情况下linux的配置
一:配置的大纲 主要的配置有几个方面: 主机名 IP 网络映射 增加新用户 给新用户root的权限,方便实验 关闭防火墙 安全子系统需要关闭 二:主机名的配置 命令:vi /etc/sysconfig ...
- 【Android测试】【随笔】模拟长按电源键
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5195121.html 起因 昨天群里看到有人问如何实现一个 ...
- java不常用语法汇总(jdk1.6)
1.浮点数省略的0 System.out.println(.5f); //.5和0.5等价. 2.import static引入一个static method后,可以在这个类中直接使用这个method ...
- iOS开发教程之:iPhone开发环境搭建
安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB以上内存的PC. 注意:运行MAC OS,需要电脑支持虚拟技术(VT),安装时,需要将VT启动,在BIOS中开启. 软件: Window ...
- JS之toString方法
1.JS中几乎每个值都有toString方法,null和undefined除外 2.对于字符串形式的值也可以使用toString()方法,返回该字符串的一个副本 3.toString(radix)方法 ...