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. 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的更多相关文章
- 【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 ...
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
- LeetCode--No.004 Median of Two Sorted Arrays
4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
随机推荐
- Flutter 1.22版本新增的Button
Flutter 1.22版本新增了3个按钮,TextButton.OutlinedButton.ElevatedButton,虽然以前的Button没有被废弃,但还是建议使用新的Button. 为什么 ...
- C++代码雨
闲逛的时候发现了一个很好玩的程序 摘自:https://blog.csdn.net/u012837895/article/details/20849967#comments 效果如下 #include ...
- Photoshop如何安装蓝湖插件
Photoshop如何安装蓝湖插件 下载蓝湖插件 直通车:蓝湖Photoshop插件: Photoshop版本要求为cc2017以上, 下载后是一个zip格式的文件,我们需要解压. 下载的文件 解压后 ...
- 02_tcp_deadlock
# 这个程序我们是测试客户端和服务端在进行通信的过程中,可能会产生死锁的情况. # 这是因为缓冲区,和TCP协议的可靠性连接导致的. # 在程序中我们可以看到,客户端先向服务端发送数据,然后服务端就收 ...
- Hill密码解密过程(Java)
Hill密码是一种传统的密码体系.加密原理:选择一个二阶可逆整数矩阵A称为密码的加密矩阵,也就是这个加密体系的密钥.加密过程: 明文字母依次逐对分组,例如加密矩阵为二阶矩阵,明文就两个字母一组,如果最 ...
- 一步一步实现直播软件源码的RTMP推流流媒体服务
第一步:准备工具 OBS推流工具下载及配置可以参见:OBS推流工具 第二步:安装流媒体服务 Windows/Linux系统环境中搭建直播流媒体服务 极速安装,下载解压一键启动即可,支持Windows和 ...
- node转发请求 .csv格式文件下载 中文乱码问题 + 文件上传笔记
用户无法直接访问后台接口 需要node端转发请求 并将数据以.csv文件格式生成以供客户端下载. 很不幸出现了中文乱码的问题 挖了各种坟帖,下了各种依赖包,csv.json2csv.bufferHel ...
- Collection迭代器Iterator的使用
package com.cx.Collecion; import java.util.ArrayList; import java.util.Collection; import java.util. ...
- day89:luffy:使用Celery完成我的订单超时取消&Polyv视频加密播放
目录 1.我的订单超时取消 2.PoliV视频播放 1.我的订单超时取消 使用Celery完成超时取消功能 mycelery/order/tasks.py from mycelery.main imp ...
- /etc/resolv.conf文件自动恢复的解决方法
/etc/resolv.conf文件自动恢复的解决方法: service NetworkManager stop #后台进程关闭 chkconfig NetworkManager off #配置关闭, ...