LeetCode Algorithm 04_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)).
Tags: Divide and Conquer, Array, Binary Search
分析:
对于数字个数k,如果k为奇数,则k个数的中位数为第(k/2+1)个;对于偶数,中位数为第k/2和(k/2+1)的平均数。
而对于两个有序数组a[m] b[n],找其第i个数的方法是:第一个数组取出前i/2个数,第二个数组则取出前(i-i/2)个数(注意不一定等于i/2),然后比较各自最大的数,如果a[i/2-1]<b[i-i/2-1],则a数组的前i/2个数全是第i个数之前的数,然后从a数组剩下(m-i/2)个数的子数组和b数组中继续取第(i-i/2)个数。
当然,这里可能会出现m<i/2的情形,这时a就取出其前m个数,b取出前i-m个数即可。对称的情况也可能出现在b上面,这时只需要更换下参数顺序即可。
最后说下终止条件,当m全取完即m==0时,就返回b[i-1]。当i/2-1<0即i<2时,无法继续细分(因为细分后要比较a[i/2-1]和b[i-i/2-1]),说明只需要从两个数组中取出第一个,则返回min(a[0],b[0])即可。
c++代码:
class Solution {
public:
double findIth(int A[], int m, int B[], int n, int i) {
if (m > n)
return findIth(B, n, A, m, i);
if (m == ) {
return B[i - ];
}
if (i <= ) {
return min(A[], B[]);
}
int aSeg = min(m, i / ), bSeg = i - aSeg;
if (A[aSeg - ] < B[bSeg - ]) {
return findIth(A + aSeg, m - aSeg, B, n, i - aSeg);
} else if (A[aSeg - ] > B[bSeg - ]) {
return findIth(A, m, B + bSeg, n - bSeg, i - bSeg);
} else {
return A[aSeg - ];
}
}
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int num = m + n;
if (num % == ) {
return findIth(A, m, B, n, num / + );
} else {
return (findIth(A, m, B, n, num / + )
+ findIth(A, m, B, n, num / )) / ;
}
}
};
LeetCode Algorithm 04_Median of Two Sorted Arrays的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — 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 两个有序数组的中位数
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
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- 【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之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
- [LeetCode][Python]Median of Two Sorted Arrays
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...
随机推荐
- WMI获取进程CPU占用率
Monitor % Process CPU Usage (specific process) http://www.tek-tips.com/viewthread.cfm?qid=395765 for ...
- cogs 1456. [UVa 10881,Piotr's Ants]蚂蚁
1456. [UVa 10881,Piotr's Ants]蚂蚁 ★ 输入文件:Ants.in 输出文件:Ants.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述 ...
- Eclipse语言的切换方法
安装完中文语言包之后,如果想切换回英文,可以按照下边的方法来做: 创建一个快捷方式,然后鼠标邮件这个快捷方式,在属性里加入-nl "en_US" 记得加空格.应用就可以. 同理切换 ...
- Android底层驱动开发(一)
1 Android为什么要增加硬件抽象层HAL A 统一硬件调用接口.所以利用HAL屏蔽linux驱动的复杂不统一的接口 B 解决GPL版权问题,因为linux内核基于GPL协议.这个G ...
- PIM-DM协议内核触发机制及协议执行机制记录
PIM-DM和PIM-SM是组播路由ASM(随意信源组播)中的两种不同模式.相对PIM-SM的组播注冊机制.PIM-DM的扩散机制显得更加粗犷. 一.PIM-DM无需向内核注冊pimreg虚接口. 可 ...
- 一个project师该怎样高效工作
1. 静. 在千头万绪,百般push.各种IM电话邮件狂轰滥炸中保持一个静字.找到最适合如今做的事情,情绪不要被外界所干扰.一次仅仅做一件事,不要被打断. 有的公司土鳖文化严重,领导一会儿要求你干这 ...
- js -- 分页功能
html 代码 <html> <head> <meta charset='utf-8'> <script type="text/javascript ...
- XHTML是什么?XHTML与HTML的区别
经常看到web前端开发人员口中提到XHTML,相信很多web前端开发的新手们感到很疑惑,甚至有些时候认为XHTML就是HTML,这个观点是错误的,今天零度就给大家好好说道说道XHTML和HTML的区别 ...
- 62.C++文件操作list实现内存检索,实现两千万数据秒查
1 #include <iostream> #include <fstream> #include <cstdlib> #include <string> ...
- 常用的130个vim命令
最近VIM用的也越来越多了...因为确实在慢慢的把win下的编辑习惯转成unix下的编辑习惯..._vimrc也在不断的完善中先贴一下平时在VIM中使用中的命令...有很多也是我没有掌握的 (估计也是 ...