学习了扁扁熊的题解: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. 关于几类STL容器swap的复杂度问题

    \(swap\)的方式有 \(S1.swap(S2)\) 或 \(swap(S1,S2)\) \(vector,map,set,deque \ \ \ \ swap\)复杂度:\(O(1)\) \(p ...

  2. [LeetCode] 141. Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  3. [LeetCode] 76. Minimum Window Substring 最小窗口子串

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  4. monkey-api-encrypt 1.1.2版本发布啦

    时隔10多天,monkey-api-encrypt发布了第二个版本,还是要感谢一些正在使用的朋友们,提出了一些问题. GitHub主页:https://github.com/yinjihuan/mon ...

  5. 日均5亿查询量的京东订单中心,为什么舍MySQL用ES?

    阅读本文大概需要 8 分钟. 来源:京东技术订阅号(ID:jingdongjishu) 作者:张sir   京东到家订单中心系统业务中,无论是外部商家的订单生产,或是内部上下游系统的依赖,订单查询的调 ...

  6. 雪花算法(snowflake)的JAVA实现

    snowflake算法由twitter公司出品,原始版本是scala版,用于生成分布式ID,结构图: 算法描述: 最高位是符号位,始终为0,不可用. 41位的时间序列,精确到毫秒级,41位的长度可以使 ...

  7. Where are registered servers stored?

    https://stackoverflow.com/questions/3064289/where-are-registered-servers-stored   They are kept as a ...

  8. SpringBoot第十五篇:swagger构建优雅文档

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11007470.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   前面的十四 ...

  9. Ubuntu 16 PPA源管理(查询、添加、修改、删除)

    查询 在Ubuntu中,每个PPA源是单独存放在/etc/apt/sources.list.d/文件夹中的,进入到该文件夹,使用ls命令查询即可列出当前系统添加的PPA源. 添加 sudo add-a ...

  10. 一步步在 github pages 上用 jekyll 搭建属于自己的博客

    序 我的专业与互联网没有太大关系,接触博客还是工作以后的事情.随着工作的经验增加,总想将自己的所思所得记录下来,毕竟,好记性不如烂笔头. 开始是将自己的总结在本地保存,但是本地有一个劣势,就是不能随时 ...