[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包 ...
随机推荐
- 协处理器,王明学learn
协处理器 协处理器用于执行特定的处理任务,如:数学协处理器可以控制数字处理,以减轻处理器的负担.ARM可支持多达16个协处理器,其中CP15是最重要的一个. CP15提供16组寄存器 通过提供的16组 ...
- 搜索 录音功能 Android api
搜索 录音功能 Android api http://www.oschina.net/android/47/ http://m.blog.csdn.net/blog/u011309801/406523 ...
- HTTP访问的两种方式(HttpClient+HttpURLConnection)整合汇总对比(转)
在Android上http 操作类有两种,分别是HttpClient和HttpURLConnection,其中两个类的详细介绍可以问度娘. HttpClient: HttpClient是Apache ...
- 在Salesforce中对某一个Object添加自定义的Button和Link
在Salesforce中可以对某一个Object添加自定义的Button和Link,来完成特定的逻辑过程,接下来以一个简单的实例来描述整个处理流程,实现的基本功能和我另外一篇文章中描述的功能是一致的( ...
- 德飞莱STM32单片机学习(一)——下载环境搭建
一.下载驱动安装. 1.手动打开CH341 文件夹(驱动程序文件夹内) ,双击安装驱动 2. 尼莫M3S 开发硬件设置 硬件需要做到以下2 点:1. USB插入USB1(COM),打开电源开关J14( ...
- form表单验证和事件
1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...
- Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件
1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...
- Hibernate,一对一外键单向 记录。Timestamp 的一个坑。
首先是2张表 表A: 表B: 其中表B中的FormBaseId对应表A中的SubjectID. 数据库中没有设置外键关系. 下面是2个对应的实体 package questionnaire.model ...
- JavaScript 笔记 ( Prototype )
这阵子实在好忙 ( 这样说好像也不是一两个月了... ),然后因为工作伙伴都是 JavaScript 神之等级的工程师,从中也学到不少知识,毕竟就是要和强者工作才会成长呀!为了想好好瞭解他们写的程式码 ...
- BestCoder Round #74 (div.2)
组合 1001 LCP Array 第一题就小难,出题的好像是浙大的大牛? 找到一个规律:a[i] = x, s[i..i+x]都想同.a[i] = a[i+1] + 1 (a[i] > 0), ...