n1 为 num1的 len

n2 为 num2的 len

故中间的数应该是 k = (n1 + n2 + 1) / 2

二分 num1中位置 m1 , 故 num2的位置为m2

必须保证 nums1[m1-1] <= nums2[m2]

且nums1[m1] >= nums2[m2-1]

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if(nums1.size() > nums2.size())
return findMedianSortedArrays(nums2, nums1);
int n1 = nums1.size(), n2 = nums2.size();
int l = 0, r = n1-1;
const int k = (n1 + n2 + 1) / 2;
while(l <= r) {
int m1 = (l + r) /2;
int m2 = k - m1;
if(nums1[m1] <= nums2[m2-1])
l = m1 + 1;
else
r = m1 - 1;
}
int m1 = l;
int m2 = k - l;
int c1 = max( m1<=0 ? INT_MIN : nums1[m1-1],
m2<=0 ? INT_MIN : nums2[m2-1]);
int c2 = min( m1>=n1 ? INT_MAX : nums1[m1],
m2>=n2 ? INT_MAX : nums2[m2] );
if((n1 + n2) & 1)
return c1;
return 0.5 * (c1 + c2);
}
};

leetcode 04 Median of Two Sorted Arrays的更多相关文章

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

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

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

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

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

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

  5. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  6. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  7. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  8. Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)

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

  9. LeetCode 004 Median of Two Sorted Arrays

    题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...

随机推荐

  1. uvalive 3353 Optimal Bus Route Design

    题意: 给出n个点,以及每个点到其他点的有向距离,要求设计线路使得每一个点都在一个环中,如果设计的线路拥有最小值,那么这个线路就是可选的.输出这个最小值或者说明最小线路不存在. 思路: 在DAG的最小 ...

  2. JS中的函数节流throttle详解和优化

    JS中的函数节流throttle详解和优化在前端开发中,有时会为页面绑定resize事件,或者为一个页面元素绑定拖拽事件(mousemove),这种事件有一个特点,在一个正常的操作中,有可能在一个短的 ...

  3. How to install john deere service advisor 4.2.005 on win 10 64bit

    How to install john deere service advisor 4.2.005 with the February 2016 data base disks on a machin ...

  4. Python+OpenCV图像处理(十)—— 图像二值化

    简介:图像二值化就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果的过程. 一.普通图像二值化 代码如下: import cv2 as cv import numpy ...

  5. 介绍Python中6个序列的内置类型

    1.Python中6个序列的内置类型分别是什么? Python包含6中内建的序列,即列表.元组.字符串.Unicode字符串.buffer对象和 xrange 对象.序列通用的操作包括:索引.长度.组 ...

  6. 洛谷 P1106 删数问题

    一定要认真审题   “去掉其中任意k个数字后剩下的数字按原左右次序将组成一个新的正整数”   也就是说 输入:7893400   4     输出:300  (00在原顺序中位于3后边) 输入:789 ...

  7. 在centos上搭建Git服务器

    第一步:先安装一些相关依赖库和编译工具 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel yum in ...

  8. The Little Prince-11/30

    The Little Prince-11/30 Today, I have a meeting in our department. I sincerely hope that all of my d ...

  9. kivy 笔记

    没有引入App对象,就不会得到一个窗口. kvlanguage用来构建UI界面,这个文件保存成”.kv”. kivy用widget来描述UI元素,lable.layout等都是widget 简单一点的 ...

  10. pxc 集群节点被kill -9 了拿什么拯救?

    集群关机或者异常宕机,重启后想要以IST的方式加入集群,需要考虑集群中是否存在满足的节点,该节点的gcache还存放着停机期间所产生的事物. 重新初始化节点加入集群应该是最后的救命稻草. 这里模拟某个 ...