4. Median of Two Sorted Arrays (二分法;递归的结束条件)
2024-08-31 01:35:00
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
}
最新文章
- mysql插入多条数据时间复杂度比较
- 我也来谈一谈c++模板(一)
- [翻译][erlang]cowboy路由模块使用
- 【mysql】统计库、表大小
- C++纯虚函数
- What are the main disadvantages of Java Server Faces 2.0?
- 一个酷炫的,基于HTML5,Jquery和Css的全屏焦点图特效,兼容各种浏览器
- Android wakelock机制
- Memory Region
- python 调用图灵机器人api实现简单的人机交互
- 第二次作业:结对编程,四则运算的GUI实现
- Go从三个站点中返回响应最快的
- 理论铺垫:阻塞IO、非阻塞IO、IO多路复用/事件驱动IO(单线程高并发原理)、异步IO
- MUI初学1
- URL 链接中 井号#、问号?、连接符&; 分别有什么作用?
- Linux 重装系统 连接不上的问题
- JSON.stringify与JSON.parse
- [学习笔记]状压dp
- ShardedJedis的分片原理
- 退役 AFO
热门文章
- Spring boot 注册Filter , Listener, Servlet
- Choose unique values for the &#39;webAppRootKey&#39; context-param in your web.xml files!
- 页面ajax自带的访问后台时,正在加载中
- ubuntu 使用命令行登录oracle
- css:margin和padding的百分之使用
- 2312--1.3.4 Prime Cryptarithm 牛式
- intellij idea 配置web 项目
- 如何遍历Set对象
- DNS协议工作过程;DNS的安全隐患
- C# 使用post的方式提交raw格式的数据,数据为json格式,多层嵌套