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 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
随机推荐
- C# 2 新增语法特性
C# 2.0 ,.NET Framework 2.0,.NET Framework 3.0,Visual Studio 2005 C#2主要添加了泛型.匿名方法,分部类型(类.结构.接口),可空类型, ...
- 1_Two Sum
1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...
- C++动态存储方式与静态存储方式
如果从变量值存在的时间(即生存期)来分,可将程序中的变量分为:动态存储方式和静态存储方式.它们所占用的存储空间区域不同. C++存储空间区域 代码区:存放可执行程序的程序代码.静态存储区:存放静态变量 ...
- Hive Join优化
在阐述Hive Join具体的优化方法之前,首先看一下Hive Join的几个重要特点,在实际使用时也可以利用下列特点做相应优化: 1. 只支持等值连接 2. 底层会将写的HQL语句转换为MapRed ...
- layui导航
关于导航 首先看一下官网的样式: <!DOCTYPE html><html><head> <meta charset="utf-8" /& ...
- Python - Git仓库忽略提交规则 & .gitignore配置
Git 忽略文件提交的方法 有三种方法可以实现忽略Git中不想提交的文件. 在Git项目中定义 .gitignore 文件 这种方式通过在项目的某个文件夹下定义 .gitignore 文件,在该文件 ...
- Docker(32)- 如何修改 docker 容器的启动参数
如果你还想从头学起 Docker,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1870863.html 前言 有时候创建容器时忘了添加 ...
- 淘宝客?CPS技术是怎么实现的?
前言 微信搜[Java3y]关注这个有梦想的男人,点赞关注是对我最大的支持! 文本已收录至我的GitHub:https://github.com/ZhongFuCheng3y/3y,有300多篇原创文 ...
- linx 内核 并发与同步 1
内核并发来源: 1.硬件中断和异常:中断服务程序和被中断的进程可能发生并发访问资源 2.软中断和tasklet,软中断和taklet随时都可能倍调度执行,从而打断当前正在执行 进程的上下文. 3.内核 ...
- java 基础知识(java web 方面的)
1.java面向对象的基本特征:封装性,多态性,继承性. 2.Java的泛型:iterator接口主要有hasnext()方法,next()方法,remove()方法:collection接口继承了i ...