[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包 ...
随机推荐
- OpenMesh 读写网格控制(读取写入纹理坐标,法向等)
OpenMesh读取网格默认是不自动读取obj网格中的法向,纹理坐标等信息的,写入网格同样也是.所以要读取(或写入)这些信息需要修改默认的选项. 先看一下其读写网格的函数 template<cl ...
- C/C++面试题
第一部分:基本概念及其它问答题 1. 关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量 ...
- 注解:【无连接表的】Hibernate单向N->1关联
Person与Address关联:单向N->1,[无连接表的] Person.java package org.crazyit.app.domain; import javax.persiste ...
- WCF服务寄宿应用程序
1.先创建一个WCF服务库 2.创建一个Console控制台,服务将寄宿在该应用程序上,该程序一旦关闭,服务将停止. 控制台代码: using System; using System.Collect ...
- oc中定时器的基本使用
// 时间间隔 调用的对象 调用的方法 用户信息 是否循环 [NSTimer scheduledTimerWithTimeInterval: target:self selector:@select ...
- Base64编码保存到文件服务器
byte[] buffer = Convert.FromBase64String(param.Base64Code); System.Net.WebClient webClient = new Sys ...
- try catch finally的一些用法
一:例题: package test; import javax.swing.*; class AboutException { public static void main(String[] a) ...
- Python Web 开发的十个框架【转载】
Python 是一门动态.面向对象语言.其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性.除了语言本身的设计目的之外,Python标准 库也是值得大家称赞的,Python甚至还 ...
- 分享Kali Linux 2016.2第47周虚拟机
分享Kali Linux 2016.2第47周虚拟机该虚拟机使用Kali Linux 2016.2第47周的64位镜像安装而成.基本设置如下:(1)该系统默认设置单CPU双核,内存为2GB,硬盘为50 ...
- 为什么下载APP,扫描二维码,关注微信公众号,就会送牛奶送小礼品?下载使用量高,会怎样?
以前的老办法是到处贴广告,电视上,广播上各种宣传. 在互联网时代,企业要盈利,除了不断优化升级自己的产品和服务,大量推广宣传产品,还要懂得用户思维.现在有网站,有APP,有微信,有二维码,可以卖产品, ...