[LintCode] 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.
Given A=[1,2,3,4,5,6]
and B=[2,3,4,5]
, the median is 3.5
.
Given A=[1,2,3]
and B=[4,5]
, the median is 3
.
The overall run time complexity should be O(log (m+n))
.
LeetCode上的原题,请参见我之前的博客Median of Two Sorted Arrays。
解法一:
class Solution {
public:
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: a double whose format is *.5 or *.0
*/
double findMedianSortedArrays(vector<int> A, vector<int> B) {
int n1 = A.size(), n2 = B.size();
if (n1 < n2) return findMedianSortedArrays(B, A);
if (n2 == ) return ((double)A[(n1 - ) / ] + (double)A[n1 / ]) / ;
int left = , right = n2 * ;
while (left <= right) {
int mid2 = (left + right) / ;
int mid1 = n1 + n2 - mid2;
double L1 = mid1 == ? INT_MIN : A[(mid1 - ) / ];
double L2 = mid2 == ? INT_MIN : B[(mid2 - ) / ];
double R1 = mid1 == n1 * ? INT_MAX : A[mid1 / ];
double R2 = mid2 == n2 * ? INT_MAX : B[mid2 / ];
if (L1 > R2) left = mid2 + ;
else if (L2 > R1) right = mid2 - ;
else return (max(L1, L2) + min(R1, R2)) / ;
}
return -;
}
};
解法二:
class Solution {
public:
/**
* @param A: An integer array.
* @param B: An integer array.
* @return: a double whose format is *.5 or *.0
*/
double findMedianSortedArrays(vector<int> A, vector<int> B) {
int m = A.size(), n = B.size(), left = (m + n + ) / , right = (m + n + ) / ;
return (findKth(A, B, left) + findKth(A, B, right)) / 2.0;
}
double findKth(vector<int> A, vector<int> B, int k) {
int m = A.size(), n = B.size();
if (m > n) return findKth(B, A, k);
if (m == ) return B[k - ];
if (k == ) return min(A[], B[]);
int i = min(m, k / ), j = min(n, k / );
if (A[i - ] > B[j - ]) {
return findKth(A, vector<int>(B.begin() + j, B.end()), k - j);
} else {
return findKth(vector<int>(A.begin() + i, A.end()), B, k - i);
}
return -;
}
};
[LintCode] Median of Two Sorted Arrays 两个有序数组的中位数的更多相关文章
- [LeetCode] 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 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 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 ...
- Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 你可以假设 nums1 和 nums2 不同 ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数
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 两个有序数组中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 4. Median of Two Sorted Arrays(2个有序数组的中位数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- Median of Two Sorted 求两个有序数组的中位数
中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...
随机推荐
- C#实现序列化和反序列化
从我们面试准备上面,我知道了一个知识点,就是我们vs提供的序列化方法有两个,一个叫二进制序列化,一个叫做xml序列化,下面我们说一下二进制序列化的C#实现: 反序列化: public static T ...
- Jquery禁止/恢复按钮与文本框代码
最近,加入了一个小项目,由浩哥带领我们几个人一起开发一个东西.幸运的是,我和胡志婷分到了一组,她可是一个具有丰富经验的牛人,对我也很好,哈哈. --背景 说点正事,最近,我们在进行项目的时候,提到了一 ...
- mysql 导出表结构和表数据 mysqldump用法
mysql 导出表结构和表数据 mysqldump用法 命令行下具体用法如下: mysqldump -u用戶名 -p密码 -d 数据库名 表名 > 脚本名; 导出整个数据库结构和数据mysq ...
- PB之入门-itemchanged(long row,dwobject dwo,string data)
每天的总结都是必须,好记性不如烂笔头,好吧,一星期没做笔记了,最近忙上PB了,哎东学学西学学,最可怕的就是最后都半斤八两,吐槽一下关于PB的资源为何如此之少,今天记录的是关于itemchanged事件 ...
- 数据库分库分表sharding1
sharding Vertical Sharding 把数据分散到多台物理机(我们称之为Shard) 实现Sharding需要解决一系列关键的技术问题,这些问题主要包括:切分策略.节点路由.全局主键生 ...
- 安卓微POS-PDA手持终端,支持离线在线联网销售开单;移动开单 盘点 功能
采购单.采购退货单 销售单.销售退货单.收款.优惠.赠品等操作实现盘点作业(多台设备同时作业,相同商品,数量累计) 现场打印票据 实现采购订单.采购单.采购退货单.销售订单.销售单.销售退货单验货没 ...
- dubbo服务治理框架设计
dubbo.JSF作为使用最广泛的服务端框治理架,其设计和实现思想值得进行学习研究. 整个服务管理框架核心的原理基于反射以及socket调用实现,服务管理框架包含服务的注册管理 服务的索引管理以及服务 ...
- WPF ,listbox,平滑滚动的2种方式。
一,烤地瓜版本的.. 这个版本不安装内容滚动,,鼠标滑轮滚动一次距离相同, 具体步骤参照他的博客,说点注意的,, 1,ScrollViewer.CanContentScroll="Fals ...
- CSS3实现动画
CSS3实现一个简单的动画 可以改变任意多的样式任意多的次数,用百分比来规定变化发生的时间,或用关键词 "from" 和 "to",等同于 0% 和 100%. ...
- Python基础10- 函数之内部函数与强制转换
#coding=utf-8 import mathimport osimport socket Python函数包括以下三类:内部函数.第三方函数和自定义函数内部函数包括:字符函数库.数学函数库.系统 ...