LeetCode: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)).

求两个有序数组的中位数,如果总元素个数是偶数,中位数是中间两个元素的平均值

 

详细讲解请参考我的另一篇博文:求两个有序数组的中位数或者第k小元素,那篇博文中如果数组总元素个数是偶数,求的是上中位数。

对于那篇博文中的算法2,这一题不能简单的套用,因为如果按照算法2的删除规则,当元素个数是偶数时,有可能把某个中位数删除了,比如对于数组【3,4】,【1,2,5,6】,比较4、5后就会把第一个数组中的3删除,但是3是要参与中位数的计算的。因此对于偶数个数的数组,我们加了一些判断,但是很复杂,代码如下,这里不推荐这种做法

  View Code

我们可以对那篇博客中算法2稍作修改就可以求下中位数,因此当两个数组总元素时偶数时,求上中位数和下中位数,然后求均值

  View Code

最优雅的方法是调用那篇博客中算法3求两个有序数组第k小元素的方法            本文地址

复制代码
 1 class Solution {
 2 public:
 3     double findMedianSortedArrays(int A[], int m, int B[], int n) {
 4         int mid_a = m/2, mid_b = n/2;
 5         if(m == 0)
 6         {
 7             if(n % 2 == 0)
 8                 return (B[mid_b] + B[mid_b-1]) / 2.0;
 9             else return B[mid_b];
10         }
11         else if(n == 0)
12         {
13             if(m % 2 == 0)
14                 return (A[mid_a] + A[mid_a-1]) / 2.0;
15             else return A[mid_a];
16         }
17         
18         if((m+n) % 2)
19             return findKthSmallest(A, m, B, n, (m+n+1)/2);
20         else return (findKthSmallest(A, m, B, n, (m+n)/2) + findKthSmallest(A, m, B, n, (m+n)/2+1)) / 2.0;
21     }
22     //找到两个有序数组中第k小的数,k>=1
23     int findKthSmallest(int vec1[], int n1, int vec2[], int n2, int k)
24     {
25         //边界条件处理
26         if(n1 == 0)return vec2[k-1];
27         else if(n2 == 0)return vec1[k-1];
28         if(k == 1)return vec1[0] < vec2[0] ? vec1[0] : vec2[0];
29         
30         int idx1 = n1*1.0 / (n1 + n2) * (k - 1);
31         int idx2 = k - idx1 - 2;
32      
33         if(vec1[idx1] == vec2[idx2])
34             return vec1[idx1];
35         else if(vec1[idx1] < vec2[idx2])
36             return findKthSmallest(&vec1[idx1+1], n1-idx1-1, vec2, idx2+1, k-idx1-1);
37         else
38             return findKthSmallest(vec1, idx1+1, &vec2[idx2+1], n2-idx2-1, k-idx2-1);
39     }
40 };
复制代码





本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3675220.html,如需转载请自行联系原作者

目录
相关文章
|
5月前
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
16 0
|
5月前
Leetcode Find Minimum in Rotated Sorted Array 题解
对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数,注意,K有可能是0,也就是没有翻转。
18 0
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
58 0
|
算法 测试技术
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
|
存储 算法
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
LeetCode 350. 两个数组的交集 II ntersection of Two Arrays II
LeetCode 350. Intersection of Two Arrays II
给定两个数组,编写一个函数来计算它们的交集。
47 0
LeetCode 350. Intersection of Two Arrays II
|
Python
LeetCode 349. Intersection of Two Arrays
给定两个数组,编写一个函数来计算它们的交集。
51 0
LeetCode 349. Intersection of Two Arrays
|
算法
LeetCode Find Minimum in Rotated Sorted Array II
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 注意数组中可能存在重复的元素。
63 0
LeetCode Find Minimum in Rotated Sorted Array II
LeetCode 153. Find Minimum in Rotated Sorted Array
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 你可以假设数组中不存在重复元素。
79 0
LeetCode 153. Find Minimum in Rotated Sorted Array