题目描述: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 arrays. The overall run time complexity should be O(log (m+n)).

分析:

题目中的数组A和数组B都是排好序的,首先想到的算法是:设置一个计数器m,指针pA指向数组A的首地址,指针pB指向数组B的首地址。如果数组A的当前元素小,pA++,m++;如果数组B的当前元素小,pB++,m++。算法的复杂度是O(m + n)。

但是题目要求时间复杂度是O(log (m+n)),一有对数,就要用到二分法了+_+每次都排除数组一半的元素,就是O(log (m+n))了!

假设A和B的元素都大于k/2,那么比较两个数组中间的元素,即A[k/2 - 1]和B[k/2 - 1]。会有三种情况:

① A[k/2 - 1] == B[k/2 - 1]

② A[k/2 - 1] < B[k/2 - 1]

③ A[k/2 - 1] > B[k/2 - 1]

情况①,找到了第k大的元素,直接返回A[k/2 - 1]或B[k/2 - 1];

情况②,A[k/2 - 1] < B[k/2 - 1],则说明A[0]到A[k/2 - 1]不会出现第k大的元素,狠心排除掉;

情况③,A[k/2 - 1] > B[k/2 - 1],则说明B[0]到B[k/2 - 1]不会出现第k大的元素,狠心排除掉。

递归函数:

• 当 A 或 B 是空时,直接返回 B[k-1] 或 A[k-1] ;
• 当 k=1 是,返回 min(A[0], B[0]) ;
• 当 A[k/2-1] == B[k/2-1] 时,返回 A[k/2-1] 或 B[k/2-1]

代码如下:

class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) { int total = m + n; if(total & 0x1) return find_kth(A, m, B, n, total / 2 + 1);
else return (find_kth(A, m, B, n, total / 2)
+ find_kth(A, m, B, n, total / 2 + 1))/2.0; } private:
static int find_kth(int A[], int m, int B[], int n, int k){ //always assume that m is equal or smaller than n
if(m > n) return find_kth(B, n, A, m, k);
if(m == 0) return B[k-1];
if(k == 1) return min(A[0], B[0]); //divide k into two parts
int ia = min(k / 2, m), ib = k - ia; if (A[ia - 1] < B[ib - 1])
return find_kth(A + ia, m - ia, B, n, k - ia);
else if (A[ia - 1] > B[ib - 1])
return find_kth(A, m, B + ib, n - ib, k - ib);
else
return A[ia - 1];
}
};

LeetCode 004 Median of Two Sorted Arrays的更多相关文章

  1. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

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

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

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

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

  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. No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

  7. LeetCode--No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

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

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

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

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

随机推荐

  1. 一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试

    一:背景 1. 讲故事 每次项目预交付的时候,总会遇到各种奇葩的坑,我觉得有必要梳理一下以及如何快速解决的,让后来人避避坑,这篇就聊聊自己的所闻所遇: 我去,本地环境代码跑的哧溜,上了测试环境出问题 ...

  2. final,static,this,super 关键字总结

    一.final 关键字 final关键字主要用在三个地方:变量.方法.类. 1.对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始 ...

  3. mysql处理查询请求的步骤

    服务端处理客户端的查询请求大致需要三个步骤: 连接管理 客户端连接服务端时,服务端会为其分配一个线程,客户端断开连接不会回收线程(避免频繁创建销毁的性能问题),服务端一直等待客户端发来消息(文本消息) ...

  4. 《精通Spring4.x企业应用开发实战》第三章

    这一章节主要介绍SpringBoot的使用,也是学习的重点内容,之后就打算用SpringBoot来写后台,所以提前看一下还是很有必要的. 3.SpringBoot概况 3.1.1SpringBoot发 ...

  5. Mybatis的二级缓存、使用Redis做二级缓存

    目录 什么是二级缓存? 1. 开启二级缓存 如何使用二级缓存: userCache和flushCache 2. 使用Redis实现二级缓存 如何使用 3. Redis二级缓存源码分析 什么是二级缓存? ...

  6. HTML 5 <input> multiple 属性

    <form action="demo_form.asp" method="get"> Select images: <input type=& ...

  7. 对pipe downstream的思考&分析

       回到ngx_http_upstream_send_response,如果是buffering,就会进入后面的处理过程,准备一个ngx_event_pipe_t结构的数据,这个结构可以通过upst ...

  8. Linux mysql 修改密码 三种方式(转载)

    注明:本文为转载,原文地址:https://www.cnblogs.com/chuckjam/archive/2018/08/10/9456255.html 前言 有时我们会忘记Mysql的密码,或者 ...

  9. Spring源码之FactoryBean的实现

    https://zhuanlan.zhihu.com/p/97005407 https://blog.csdn.net/qq_35634181/article/details/104507465 总结 ...

  10. cephfs根据存储池显示df容量

    前言 如果用cephfs比较多,应该都知道,在cephfs的客户端进行mount以后,看到的容量显示的是集群的总的容量,也就是你的总的磁盘空间是多少这个地方显示的就是多少 这个一直都是这样显示的,我们 ...