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

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

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开发--网络下载

    这里使用的是NSURLConnection的代理请求下载,并且是具有进度,UI能实时刷新,至于NSURLConnection如何请求.并且有几种请求方法请看NSURLConnection请求简介,在这 ...

  2. TCL语言笔记:TCL中的String命令

    一.介绍 字符串是 Tcl 中的基本数据类型,所以有大量的字符串操作命令.一个比较重要的问题就是模式匹配,通过模式匹配将字符串与指定的模式(格式)相匹配来进行字符串的比较.搜索等操作. 二.strin ...

  3. React-用ImmutableJS提高性能

    一.需求 1.子组件有更新时,只重新渲染有变化的子组件,而不是全部 二.ImmutableJS原理 三.代码 1.CheckboxWithLabel.jsx var React = require(' ...

  4. Android 拦截短信

    public class SMSMess extends BroadcastReceiver { @Override public void onReceive(Context arg0, Inten ...

  5. Java—反射

    通过程序化的方式间接对Class的对象实例操作,Class文件由类装载器装载后,在JVM中将形成一份描述Class结构的元信息对象,通过该元信息对象可以获知Class的结构信息:如构造函数.属性和方法 ...

  6. BZOJ 2323 细胞(矩阵)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2323 题意: 题意过于复杂,我直接简化下.给出一个长度为n的数字串,只包含1到9,将数字 ...

  7. BZOJ 3142 数列(组合)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3142 题意:给出n,K,m,p.求有多少长度为K的序列A,满足:(1)首项为正整数:(2 ...

  8. 面向对象设计Object Oriented Design

    http://www.codeproject.com/Articles/93369/How-I-explained-OOD-to-my-wife http://www.cnblogs.com/niyw ...

  9. Android开发之获取系统管理权限,即DevicePolicyManager和DeviceAdminReceiver的使用

    参考:http://www.cnblogs.com/androidez/archive/2013/02/17/2915020.html 1.创建AdminReceiver,继承DeviceAdminR ...

  10. Difference between 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1

    Reference link: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-nu ...