4. 寻找两个正序数组的中位数(困难)-字节跳动高频题-二分查找
Posted hequnwang10
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了4. 寻找两个正序数组的中位数(困难)-字节跳动高频题-二分查找相关的知识,希望对你有一定的参考价值。
一、题目描述
给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
算法的时间复杂度应该为 O(log (m+n)) 。
示例 1:
输入:nums1 = [1,3], nums2 = [2]
输出:2.00000
解释:合并数组 = [1,2,3] ,中位数 2
示例 2:
输入:nums1 = [1,2], nums2 = [3,4]
输出:2.50000
解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5
二、解题
二分查找
这题有一个条件时间复杂度应该为 O(log (m+n)) ,看到这个就明白需要使用二分查找。
直接复制LeetCode官方代码。
class Solution
public double findMedianSortedArrays(int[] nums1, int[] nums2)
//使nums1长度最小
if(nums1.length > nums2.length)
int[] temp = nums1;
nums1 = nums2;
nums2 = temp;
int m = nums1.length;
int n = nums2.length;
//分割线左边的所有元素需要满足的个数 m+(n-m+1)/2;
int totalLeft = (m+n+1)/2;
//在nums1的区间[0,m]里查找恰当的分割线
//使得nums1[i-1] <= nums2[j] && nums2[j-1] <= nums1[i]
int left = 0;
int right = m;
while(left < right)
int i = left + (right - left + 1)/2;
int j = totalLeft - i;
if(nums1[i-1] > nums2[j])
//下一轮搜索的区间[left ,i - 1]
right = i - 1;
else
//下一轮的搜索区间[i,right]
left = i;
int i = left;
int j = totalLeft - i;
int nums1LeftMax = i==0 ? Integer.MIN_VALUE : nums1[i-1];
int nums1RightMin = i==m ? Integer.MAX_VALUE : nums1[i];
int nums2LeftMax = j==0 ? Integer.MIN_VALUE : nums2[j-1];
int nums2RightMin = j==n ? Integer.MAX_VALUE : nums2[j];
if(((m+n)%2) == 1)
return Math.max(nums1LeftMax,nums2LeftMax);
else
return (double) ((Math.max(nums1LeftMax,nums2LeftMax) + Math.min(nums1RightMin,nums2RightMin))) / 2;
时间复杂度是 O(log(min(m,n));
空间复杂度是 O(1)。
![](https://image.cha138.com/20220420/ef6a7f50da8a4cefbfc0c2d22bc917ef.jpg)
![](https://image.cha138.com/20220420/dff8a2a2d0e24f87b00ac80ebdec98d8.jpg)
以上是关于4. 寻找两个正序数组的中位数(困难)-字节跳动高频题-二分查找的主要内容,如果未能解决你的问题,请参考以下文章
leetcode 4. Median of Two Sorted Arrays 寻找两个正序数组的中位数(困难)
每日算法/刷穿 LeetCode4. 寻找两个正序数组的中位数(困难)