4. Median of Two Sorted Arrays (二分法;递归的结束条件)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int len = nums1Size+nums2Size;
int median = len >> ;
if(len%==) {
if(nums1Size==) return nums2[median];
if(nums2Size==) return nums1[median];
return findK(nums1, , nums1Size, nums2, , nums2Size, median+);
}
else{
if(nums1Size==) return (double)(nums2[median-]+nums2[median])/;
if(nums2Size==) return (double)(nums1[median-]+nums1[median])/;
return (double)(findK(nums1, , nums1Size, nums2, , nums2Size, median)+findK(nums1, , nums1Size, nums2, , nums2Size, median+))/;
}
}
int findK(int* nums1, int start1, int len1, int* nums2, int start2, int len2, int k){
if(len1==){ //check len = 1 case, because we keep median when recursion; otherwise, endless loop
if(nums1[start1] < nums2[start2+k-]) return nums2[start2+k-];
else{
if(k > len2 || nums1[start1] < nums2[start2+k-] ) return nums1[start1];
else return nums2[start2+k-];
}
}
if(len2==){
if(nums2[start2] < nums1[start1+k-]) return nums1[start1+k-];
else{
if(k > len1 || nums2[start2] < nums1[start1+k-] ) return nums2[start2];
else return nums1[start1+k-];
}
}
int median1 = start1+(len1 >> ); //if len is odd, it's exactly median; else if even, it's the second of the two median
int median2 = start2+(len2 >> );
if(k <= ((len1+len2)>>)){ //k is at the first half
if(nums1[median1] < nums2[median2]){ //delete the second half of nums2
findK(nums1, start1, len1, nums2, start2, median2-start2, k); //1. delete from median (including median)
}
else{//delete the second half of nums1
findK(nums1, start1, median1-start1, nums2, start2, len2, k);
}
}
else{ //k is at the second half
if(nums1[median1] < nums2[median2]){ //delete the first half of nums1
findK(nums1, median1, len1-(median1-start1), nums2, start2, len2, k-(median1-start1)); //2. Each time delete half at most, so keep median
}
else{ //delete the first half of nums2
findK(nums1, start1, len1, nums2, median2, len2-(median2-start2), k-(median2-start2));
}
}
//From 1, 2, we can see, when only one element, it cannot be deleted, so the end loop condition is len = 1
}
4. Median of Two Sorted Arrays (二分法;递归的结束条件)的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- 【LeetCode】4. Median of Two Sorted Arrays (2 solutions)
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- 4. Median of Two Sorted Arrays(topK-logk)
4. Median of Two Sorted Arrays 题目 There are two sorted arrays nums1 and nums2 of size m and n respec ...
- LeetCode 第四题 Median of Two Sorted Arrays 二人 渣渣选手乱七八糟分析发现基本回到思路1
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- [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.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
随机推荐
- leetcode987
public class Solution { private Dictionary<int, List<KeyValuePair<int,int>>> dic = ...
- <基础> PHP 数组操作
array_filter — 用回调函数过滤数组中的单元 ( 如果 callback 函数返回 true,则 array 数组的当前值会被包含在返回的结果数组中.数组的键名保留不变 ) array a ...
- php7内核执行流程(转载留记录)
- 排序NB三人组
排序NB三人组 快速排序,堆排序,归并排序 1.快速排序 方法其实很简单:分别从初始序列“6 1 2 7 9 3 4 5 10 8”两端开始“探测”.先从右往左找一个小于6的数,再从左往 ...
- MOBA项目问题记录
1,动态变化的文本,使用较多时,耗时大,原因:只要字符串发生改变就会重绘,原理:每个字符的宽度不一样 项目中使用了网上写的一个文本控件,大概类型UGUI的东西,实现了字符缓存,绘制过的字符就不会再重建 ...
- ReactiveX 学习笔记(0)学习资源
ReactiveX 学习笔记 ReactiveX 学习笔记(1) ReactiveX 学习笔记(2)创建数据流 ReactiveX 学习笔记(3)转换数据流 ReactiveX 学习笔记(4)过滤数据 ...
- Haskell语言学习笔记(86)字符串格式化与插值
String 的格式化 Text.Printf 这个模块用来处理字符串格式化. printf :: PrintfType r => String -> r printf 用于格式化字符串, ...
- 简单几步手工扩容LVM(笔记)
参考文档:https://www.cnblogs.com/einyboy/archive/2012/05/31/2528661.html 1.查看磁盘是否被系统认出: fdisk -l 如显示加的磁盘 ...
- mongoDB如何处理多对多关系
问题描述: 例如在关系数据库中有一个Team表,一个User表,两者是多对多的关系,即一个Team可以有多个User,一个User也可能属于多个Team,请问这样的关系在MongoDB中如何存储? 如 ...
- week6 10 后端backend server和mongoDB通信
0 之前我们maogoDB用的是在线的mlab 在线他们帮我们做好了model 也就是那个schma 其实python也有类似的包 帮我们定义这些model 但是呢 我们自己来做吧 用一个传统的意义上 ...