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. nw.js package一般设置

    { "name": "app name", "main": "mainpage",                    ...

  2. 7.线程id,优先级讲解

    1.线程id可以通过Thread对象的getId()方法得到,在线程出了问题,为什么CPU占用这么高的时候,查的时候我们可以在堆栈信息中找到对应线程,然后干掉该线程就好! 2.而线程对象的getNam ...

  3. RootConfig类

    package com.ssm.yjblogs.config; import java.util.Properties; import javax.sql.DataSource; import org ...

  4. GCD(Swift)

    1.取消过去的接口 说起 GCD, 大家肯定回想起类似 dispatch_async 这样的语法. GCD 的这个语法模式无论是和 Objc 还是 Swift 的整体风格都不太打掉. 所以 Swift ...

  5. 【2017-03-02】C#集合,结构体,枚举

    集合 集合与数组的区别 数组:同一类型,固定长度 集合:不同类型,不固定长度 使用集合前需要:     引用命名空间:using System.Collections; 1.普通集合 定义: Arra ...

  6. Spark学习之路 (十一)SparkCore的调优之Spark内存模型

    摘抄自:https://www.ibm.com/developerworks/cn/analytics/library/ba-cn-apache-spark-memory-management/ind ...

  7. JavaScript 函数声明与函数表达式的区别 函数声明提升(function declaration hoisting)

    解析器在向执行环境中加载数据时,对函数声明和函数表达式并非一视同仁.解析器会率先读取函数声明,并使其在执行任何代码之前可用(可以访问).至于函数表达式,则必须等到解析器执行到它所在的代码行,才会真的被 ...

  8. tar命令打包文件夹下所有的文件

    例如在/home/rip123/www 路径有aa.txt   aab.txt   bb.txt    cc.txt 文件,想将所有的打包却不想一个个敲: 做法:在www文件夹下输入命令:   tar ...

  9. python 在列表,元组,字典变量前加*号

    废话不说,直接上代码(可能很多人以前不知道有这种方法): a=[1,2,3]b=(1,2,3)c={1:"a",2:"b",3:"c"}pr ...

  10. 设计模式之Singleton(单态)(转)

    定义: Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 在很多操作中,比如建立目录 数据库连接都需要这样的单线程操作. 还有, singleton能够被状 ...