实验室太吵了。。。怎么办啊。。。

--------------------------------------------------------------------------------------------------------------------

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)).

【题意】:给你两个已经排好序的数组,求中位数,要求时间复杂度为:O(log(m+n))。

【心路历程】:看完这道题直接的想法就是用merge 的方法对两个数组合并排序,但是时间复杂度为:O((m+n))。

因为要求的时间复杂度为O(log(m+n)),所以很自然会想去用二分求解。于是,我最开始的思路是分别对两个数组进行二分,

然后进行比较,如果A > B,则去掉B的小于N/2的部分,去掉A的大于M/2的部分。如果A < B,则去掉A的小于M/2的部分,

去掉B的大于M/2的部分。如果 A = B ,则返回中间值。后来发现这个方法有很多不妥之处,自己实现时也是感觉有问题。

于是又开始想,发现自己确实没啥思路,就无耻的度娘了一下,发现这题可以转化成求最K值问题。原来在《剑指OFFER》上看过

最k值问题,但是却忘得一干二净了。。。

这题就是最K值问题,两个数组分别取第K/2个元素进行比较。如果A[K/2] < B[K/2] ,则忽略掉A[k/2]之前的所有元素;同理如果

A[K/2] > B[K/2] ,则忽略掉B[K/2]之前的所有元素;如果A[K/2] == B[K/2] ,则返回A[K/2] 或 B[K/2] 任意的一个元素

(其实这个想法,和我之前想到的那个还是蛮相似的。。。)。用递归很好实现,代码是学习别人的,感觉写的很好,比自己的写的好。

学习了,感觉自己代码实现能力还是不太好,要加强。

-----------------------------------------------------------------------------------------------------------------------------------

代码如下:

 double f(int * nums1,int m,int *nums2,int n,int k) {
if(m > n) {
return f(nums2,n,nums1,m,k);
}
if(m == ) {
return nums2[k - ];
}
if(k == ) {
if(nums1[] < nums2[]) {
return nums1[];
}else {
return nums2[];
}
}
int pa,pb;
if(k/ < m) {
pa = k/;
pb = k - pa;
}else {
pa = m;
pb = k -pa;
}
if(nums1[pa - ] < nums2[pb - ]) {
return f(nums1 + pa,m - pa,nums2 , n, k - pa);
}else if(nums1[pa - ] > nums2[pb - ]) {
return f(nums1 ,m ,nums2 + pb, n - pb, k - pb);
}else {
return nums1[pa - ];
}
} double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { int total = nums1Size + nums2Size;
if(total % == ){
return ( f(nums1,nums1Size,nums2,nums2Size,total/) + f(nums1,nums1Size,nums2,nums2Size,total/ + ) ) / ;
}else {
return f(nums1,nums1Size,nums2,nums2Size,total/ + );
}
}

一起刷LeetCode4-Median of Two Sorted Arrays的更多相关文章

  1. Leetcode4:Median of Two Sorted Arrays@Python

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  2. LeetCode4 Median of Two Sorted Arrays

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  3. leetcode4 Median of Two Sorted Arrays学习记录

    学习了扁扁熊的题解:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge- ...

  4. Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...

  5. 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

  6. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

  7. 刷题4. Median of Two Sorted Arrays

    一.题目 Median of Two Sorted Arrays,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free也不难,最大的问题是性能问题 ...

  8. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  9. [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 ...

  10. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

随机推荐

  1. iOS xcuserdata

    说明:       project.xcworkspace说明:is a directory of files describing the workspace or projects. Althou ...

  2. SVN与Eclipse整合

    SVN与Eclipse整合 下载SVN插件(http://subclipse.tigris.org) 我们使用版本eclipse_svn_site-1.6.5.zip 解压到一个文件夹中 进入ecli ...

  3. JDBC学习总结(四)

    JDBC对LOB的读写 在JDBC中提供了java.sql.Blob和java.sql.Clob,两个类分别代表BLOB和CLOB数据.         · BLOB(Binary Large Obj ...

  4. URAL 1957 Wrong Answer 暴力

    Wrong Answer 思路: 1.先枚举4的全排列,即球赛的所有可能结果,一共4!=24种情况 2.对于每种情况,DFS 未确定的比赛 的结果,判断这种情况是否可达. 剪枝: 1.对于每种全排列, ...

  5. 创建支持复杂脚本Complex Scripts的WINCE6.0系统

    如果要创建支持复杂脚本(Complex Scripts)的系统,我们需要完成下面一系列步骤来确保系统包含所有需要支持的具体区域设置 (locale–specific). 1.     选择intern ...

  6. POJ 3252 Round Numbers(组合)

    题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...

  7. Python之格式化输出讲解

    1.格式化输出整数python print也支持参数格式化,与C言的printf似, strHello = "the length of (%s) is %d" %(Hello W ...

  8. hdu 4911 Inversion (分治 归并排序 求逆序数)

    题目链接 题意:给n个数,求交换k次相邻的数之后的最小的逆序数对. 用分治的方法,以前在poj上做过这种题,昨天比赛的时候忘了.... 下面的归并排序还是以前的模板. #include <ios ...

  9. freemarker中判断对象是否为空

    <#if xxx?exists> 或则 <#if xxx??>两个问号??最简单方便

  10. Math.sqrt

    java.lang.Math.sqrt(double a) 返回正确舍入的一个double值的正平方根.特殊情况: 如果参数是NaN或小于为零,那么结果是NaN. 如果参数是正无穷大,那么结果为正无穷 ...