学习了扁扁熊的题解: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. vue项目的安装与启动

      第一步:安装Node 检测Node是否安装成功 node -v 第二步:安装vue-cli     命令:npm install vue-cli -g 第三步:搭建项目命令 vue init we ...

  2. 新电脑安装操作系统一定要注意硬盘是否被bitlocker加密!

    新电脑安装操作系统一定要注意硬盘是否被bitlocker加密! 前段时间帮一MM的戴尔灵越14燃5488装机,购买不久的电脑,硬盘是被bitlocker加密的,鬼知道戴尔为什么这么过分.按照常规思路, ...

  3. Linux性能优化实战学习笔记:第三十三讲

    一.上节回顾 前几节,我们一起学习了文件系统和磁盘 I/O 的工作原理,以及相应的性能分析和优化方法.接下来,我们将进入下一个重要模块—— Linux 的网络子系统. 由于网络处理的流程最复杂,跟我们 ...

  4. WPF CoboxItem控件使用SelectedItem去调System.Windows.Controls.ComboBoxItem: 前缀方法

    textComBox.SelectedItem as ComboBoxItem).Content textConbox: 控件Combobox 的Name 在Combobox控件SelectionCh ...

  5. [转载]3.4 UiPath键盘操作的介绍和使用

    一.键盘操作的介绍 模拟用户使用键盘操作的一种行为: 例如使用发送热键(Sendhotkey),输入信息 (Typeinto)的操作 二.键盘操作在UiPath中的使用 1.打开设计器,在设计库中新建 ...

  6. 解密httpclient,dbcp,jedis,c3p0,druid,okhttp都在使用的连接池技术

    最近在连接池上面栽了个跟头(参见这里),引起我对池技术的强烈关注,这几天总结了一下很多场景都会使用的池技术: 池概念 pool,中文翻译为水池,但是在英文中,还有一种解释是 an organizati ...

  7. Leetcode 第137场周赛解题报告

    今天的比赛的题目相对来说比较「直白」,不像前几周都是一些特定的算法,如果你没学过不可能想出来. 做了这些周,对leetcode比赛的题目也发现了一些「规律」. 一般前两道题都很「简单」,只要有想法,直 ...

  8. 为什么我的resharper控件安装之后没有显示

    Resharper和Resharper C++有时候会出现,安装之后不显示,VisualStudio菜单栏内找不到的情况,大多数是因为启动VisualStudio的时候没有激活Resharper. 安 ...

  9. 使用Django REST框架创建一个简单的Api

    Create a Simple API Using Django REST Framework in Python WHAT IS AN API API stands for application ...

  10. 高通msm8909打开debug串口

    1.修改板级文件 $ cd AOSP $ vim device/qcom/msm8909/BoardConfig.mk 如下所示: 2.修改defconfig文件 $ cd kernel/arch/a ...