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和两个堆顶的数据决定往哪个堆里面放. ...
随机推荐
- RSA
https://en.wikipedia.org/wiki/RSA_(cryptosystem)
- DefaultHttpClient使用
转自:http://username2.iteye.com/blog/1664995 博客分类: java 工具类 javaio ]; } json = JSONObject.fromObject ...
- 蓝牙—服务发现协议(SDP)
服务搜索协议(SDP)提供了应用发现可用服务以及确定可用服务特点的方法.SDP发现协议提供下面的能力 <1>为客户提供搜索所需要服务的能力. <2>允许基于服务类型搜索服务 & ...
- java整合spring和hadoop HDFS
http://blog.csdn.net/kokjuis/article/details/53586406 http://download.csdn.net/detail/kokjuis/970932 ...
- SQL Server批量数据导出导入Bulk Insert使用
简介 Bulk insert命令区别于BCP命令之处在于它是SQL server脚本语句,它可以将本地或远程的文件数据批量导入数据库,速度非常之快:远程文件必须共享才行, 文件路径须使用通用约定(UN ...
- [LeetCode]题解(python):030-Substring with Concatenation of All Words
题目来源 https://leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string ...
- iOS 给UILabel文字加下划线
摘自:http://blog.sina.com.cn/s/blog_6cd380c10101b6hn.html //带下划线的“注” NSMutableAttributedString可变的属性字符串 ...
- iOS7上TableViewCell的button和UIImageView个别未显示的bug
要做这个cell,用xib将cell做成之后,在iPhone6.6Plus.5s上运行良好,但是在iOS7的5s和iPad上,黄色的小星星和下载按钮均没有显示. 甚为惊奇. 在网上百度之,发现了解决办 ...
- C#类的继承
public class Animal { public string word=""; //virtual表示虚方法,用以被重写 public virtual void say( ...
- django 笔记
最近开始接触django,一些基本的操作记录于此. 参考链接: http://www.ziqiangxuetang.com/django/django-tutorial.html django安装 s ...