Q4:Median of Two Sorted Arrays

4. Median of Two Sorted Arrays

官方的链接:4. Median of Two Sorted Arrays

Description :

There are two sorted arrays nums1 and nums2 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)).

Example1:



nums1 = [1, 3]

nums2 = [2]

The median is 2.0



Example2:



nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5



问题描述

给定长度分别为m和n的两个排序数组nums1和nums2,在时间复杂度O(log (m+n))内算出数组的中位数

思路

两个有序数组的中位数和Top K问题类似。这里从小到大直接定位第k个,简单理解为从nums1中获取第i个,而从nums2中获取第j=k-i个,其中i=0~k。当定位到nums[i-1]<=nums2[j]和nums[j-1]<=nums[i]即完成,当然还有边界问题。

把中位数也当作是top k问题,最后进行奇偶判断。详情可参考这里Share my O(log(min(m,n)) solution with explanation,代码也是借鉴的。

值得注意的是几个边界判断问题,比如i为0或者m,j为0或者n。

[github-here]

 1 public class Q4_MedianOfTwoSortedArrays {
 2     public double findMedianSortedArrays(int[] nums1, int[] nums2) {
 3
 4         int m = nums1.length;
 5         int n = nums2.length;
 6         // make sure that m <= n
 7         if (m > n) {
 8             return findMedianSortedArrays(nums2, nums1);
 9         }
10         // n>=m,i = 0 ~ m,so j = (m + n + 1) / 2 -i > 0.
11         int i = 0, j = 0, imax = m, imin = 0, halfLen = (m + n + 1) / 2;
12         int maxLeft = 0, minRight = 0;
13         while (imin <= imax) {
14             i = (imin + imax) / 2;
15             j = halfLen - i;
16             if (i < m && nums2[j - 1] > nums1[i]) {
17                 imin = i + 1;
18             } else if (i > 0 && nums1[i - 1] > nums2[j]) {
19                 imax = i - 1;
20             } else {
21                 if (i == 0) {
22                     //the target is in nums2
23                     maxLeft = nums2[j - 1];
24                 } else if (j == 0) {
25                     //the target is in nums1
26                     maxLeft =  nums1[i - 1];
27                 } else {
28                     maxLeft = Math.max(nums1[i - 1], nums2[j - 1]);
29                 }
30                 break;
31             }
32         }
33         //odd
34         if ((m + n) % 2 == 1) {
35             return (double)maxLeft;
36         }
37         //even
38         if (i == m) {
39             //nums1 is out of index m
40             minRight = nums2[j];
41         } else if (j == n) {
42             //nums2 is out of index n
43             minRight = nums1[i];
44         } else {
45             //others
46             minRight = Math.min(nums1[i], nums2[j]);
47         }
48         return (double) (maxLeft + minRight) / 2;
49     }
50
51     public static void main(String[] args) {
52         new Q4_MedianOfTwoSortedArrays().findMedianSortedArrays(new int[] { 1, 3 }, new int[] { 2 });
53
54     }
55 }

原文地址:https://www.cnblogs.com/wpbxin/p/8654941.html

时间: 2024-08-05 19:21:12

Q4:Median of Two Sorted Arrays的相关文章

LeetCode2: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)). 解题思路: 这道题,很容易想到的是先排序再直接定位中间那个数即可,m+n为偶数的话,应为中间两数之和除2,但时间复杂度不符合题目要求,还一种方法就是利用归并思想,因

No.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 sorted arrays. The overall run time complexity should be O(log (m+n)). Example1:nums1 = [1, 3]nums2 = [2]The median is 2.0Example2:nums1 = [1, 2]n

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 sorted arrays. The overall run time complexity should be O(log (m+n)) 代码如下: class Solution { int getMedian(int A[], int m, int B[], int n,int k) { if

第四题:Median of Two Sorted Arrays

题目链接:题目链接 题意:两个排好序的数组,找到中位数,如果是奇数好办,如果是偶数找到最中间的两个求平均值. 这一题的本质其实就是第mid小的数. 这一题看到一种好的方法,我们令k=mid,对于两个数组,分别取前k/2数,如果A[k/2-1]比B[k/2-1]大,那么说明B前 k/2数肯定在k小数中,A的则不一定,则下面需要从A,B+k/2再次去寻找...反之对于B大的情况也是一样的,如果相 等,那就是这两个数(相等)了,随便返回一个都OK.(Ps:解释一些为什么是k/2-1,因为k是从1开始取

LeetCode-刷题 Median of Two Sorted Arrays

之前一直是写C++的,想着开始学学用JAVA写Code会有什么不同,是否会更加简洁?于是开始学着用JAVA去刷LEETCODE 习题如下: There are two sorted arrays nums1 and nums2 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)). Example

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 sorted arrays. The overall run time complexity should be O(log (m+n)). 解题思路: 本题要求求解的是两个有序序列的中位数.本质上就是求两个有序序列“第k小的数“的变形.假设两个有序序列一个长为m,另一个长为n,则我们

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 sorted arrays. The overall run time complexity should be O(log (m+n)). 题意: 两个排序后的数组nums1 和nums2,长度分别是m,n,找出其中位数,并且时间复杂度:O(log(m+n)) 最愚蠢的方法: 两个数组合

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 sorted arrays. The overall run time complexity should be O(log(m+n)) 题解: 1.自己想得思路:构建一个list,然后比较各数的大小,将其插入到合适的位置 class Solution: # @param {integer[

LeetCode(3) || Median of Two Sorted Arrays

LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题目内容 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