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

记录一下,顺便按自己的理解给代码加上注释

#include <vector>
#include <stdio.h>
using namespace std;
#define max(a,b) (((a)>(b)) ? (a) : (b) )
#define min(a,b) (((a)<(b)) ? (a) : (b) )
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m=nums1.size();
int n=nums2.size();
//保持数组1为短的是为了提高效率,其实只要选定一个数组二分就可以了。
if(m>n)
{
return findMedianSortedArrays(nums2,nums1);
}
//lo是数组的左部,hi是数组的末尾,为了防止奇偶问题,把数组长度已经变成2m+1和2n+1
//数组末尾应该是2m+1,下标从0开始,所以hi是2m
int LMax1,LMax2,RMin1,RMin2,c1,c2,lo=0,hi=m*2;
//开始二分数组1
while(lo<=hi)
{
c1=(lo+hi) /2 ;
c2 = m+n - c1; //因为分别数组扩大到了2倍,所以是中位数在m+n+1 即(2m+1+2n+1)/2
//我们知道要保持从c1+c2=k,注意m+n就是相当于m+n+1(下标从0开始) //当数组1二分完了,如果遇到割在数组的两边,说明整个数组都是大于或者小于目标中位数的
//这个时候数组1的LMax和RMin是不能参与计算最后的中位数
//所以要特殊处理一下,例如c1==0,说明数组1整个都大于目标中位数,那么LMax1一定会大于LMax2
//可我们不希望LMax1作为结果的一部分,结果应该是由LMax2和RMin2计算出来的,所以此时强行把LMax1=INT_MIN
//这样最后求中位数,就Max(LMax1,LMax2)就不会返回LMax1而是LMAx2。别的情况以此类推
LMax1=(c1==0)?INT_MIN:nums1[(c1-1)/2];
RMin1=(c1==2*m)?INT_MAX:nums1[c1/2];
LMax2=(c2==0)?INT_MIN:nums2[(c2-1)/2];
RMin2=(c2==2*n)?INT_MAX:nums2[c2/2]; if(LMax1>RMin2)
{
hi=c1-1;
}else if(LMax2>RMin1)
{
lo=c1+1;
}else
break;
}
//题解里解释过,拿#填充扩大2倍的数组,中位数=(LMax+RMin)/2
return (max(LMax1,LMax2)+min(RMin1,RMin2))/2.0;
}
};

主要是记录一下,学习完扁扁熊题解的感悟,顺便写一下大佬没详细说明的部分。

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两个排序数组的中位数

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

  4. 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)

    转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...

  5. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  6. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

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

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

随机推荐

  1. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  2. springcloud2.x之management.security.enabled=false报错处理

    1. springcloud1.5.x的消息总线配置是 # RabbitMq的地址.端口,用户名.密码 spring.rabbitmq.host=localhost spring.rabbitmq.p ...

  3. python3中用django下载文件,中文名乱码怎么办?

    前段时间被某个前端小可爱鄙视了一下,说我博客都一年不更新了,我不服,明明还有俩月才到一年呢.不过说是这么说,还是要更新一下的. 以上都是借口,下面开始正文.     我公司的某个内部系统,用djang ...

  4. Salesforce Lightning开发学习(四)重写新建/更新按钮

    重写新建/更新按钮的原因是因为项目需要用户在新建数据时从接口对数据进行校验,保证数据的有效性,同时获取接口返回的部分数据完成信息填充,而Sales force的trigger仅支持@future方法异 ...

  5. 我已经看到了,撤回也没用了(PC微信防撤回补丁)

    前两天看 GitHub 发现一个有趣的项目,PC微信防撤回补丁,本着研究学习的目的,在看过源码,一顿疯狂操作之后,了解了其原理是基于修改 wechatwin.dll 达到防撤回的. 于是乎,自己动手玩 ...

  6. Laravel源码解析之model(代码)

    本篇文章给大家带来的内容是关于Laravel源码解析之model(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 前言 提前预祝猿人们国庆快乐,吃好.喝好.玩好,我会在电视上看 ...

  7. MySQL 只能做小项目?松哥要说几句公道话!

    松哥上学那会,很多人对 MySQL 有一些偏见,偏见主要集中在以下几方面: MySQL 不支持事务(事实上 MyISAM 有表锁,但是效率比较低) MySQL 存储的数据量比较小,适合小项目,大项目还 ...

  8. W tensorflow/core/util/ctc/ctc_loss_calculator.cc:144] No valid path found 或 loss:inf的解决方案

    基于Tensorflow和Keras实现端到端的不定长中文字符检测和识别(文本检测:CTPN,文本识别:DenseNet + CTC),在使用自己的数据训练这个模型的过程中,出现如下错误,由于问题已经 ...

  9. 《 .NET并发编程实战》实战习题集 - 2 - 替换算法

    先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.

  10. 为什么改了JS数组中的一个元素的值,其他数组元素值都跟着变了

    原因: 数组是引用类型,数组变量存储在栈,元素数据存储在堆中,将数组赋值不同的对象,所以的赋值对象都指向堆同一个数据,所以改变其中一个数组中的元素,别的数组元素也会改变. 解决方案: 原理就是先把数组 ...