当前位置:网站首页>每日一题-寻找两个正序数组的中位数-0713
每日一题-寻找两个正序数组的中位数-0713
2022-08-05 05:17:00 【菜鸡程序媛】
题目:
给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
算法的时间复杂度应该为 O(log (m+n)) 。
这个题,目前还不是很理解,为了速度,先背了…
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if(nums1.length > nums2.length)
return findMedianSortedArrays(nums2, nums1);
int m = nums1.length;
int n = nums2.length;
int left = 0, right = m;
int mid1 = 0, mid2 = 0;
while(left <= right){
int i = (left + right) / 2;
int j = (m + n + 1) / 2 - i;
int nums_im1 = i == 0 ? Integer.MIN_VALUE : nums1[i - 1];
int nums_i = i == m ? Integer.MAX_VALUE : nums1[i];
int nums_jm1 = j == 0 ? Integer.MIN_VALUE : nums2[j - 1];
int nums_j = j == n ? Integer.MAX_VALUE : nums2[j];
if(nums_im1 < nums_j){
mid1 = Math.max(nums_im1, nums_jm1);
mid2 = Math.min(nums_i, nums_j);
left = left + 1;
}else{
right = right - 1;
}
}
return (m + n) % 2 == 0 ? (mid1 + mid2) / 2.0 : mid1;
}
}
边栏推荐
猜你喜欢

The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time

CVPR2020 - 自校准卷积

【Multisim仿真】直流稳压电源设计报告

Comparison and summary of Tensorflow2 and Pytorch in terms of basic operations of tensor Tensor

电子产品量产工具(3)- 文字系统实现
![[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)](/img/71/f82e76085f9d8e6610f6f817e2148f.png)
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)

OSPF网络类型

六、请求处理—获取请求参数系列注解是怎样工作的?

PoE视频监控解决方案

网络信息安全运营方法论 (下)
随机推荐
数控直流电源
[Pytorch study notes] 11. Take a subset of the Dataset and shuffle the order of the Dataset (using Subset, random_split)
LeetCode刷题之第746题
网络通信及相关函数介绍
深度学习系列(二)优化器 (Optimization)
网络信息安全运营方法论 (上)
LeetCode刷题之第54题
最简单的防抖节流理解法
【UiPath2022+C#】UiPath 练习-数据操作
The University of Göttingen proposed CLIPSeg, a model that can perform three segmentation tasks at the same time
【Promise高级用法】实现并行和串行API
[Pytorch study notes] 10. How to quickly create your own Dataset dataset object (inherit the Dataset class and override the corresponding method)
网工必用神器:网络排查工具MTR
【UiPath2022+C#】UiPath 数据操作
TinyFlashDB:一种超轻量的可纠错的通用单片机flash存储方案
每日一题-下一个排列-0723
电子产品量产工具(4)-UI系统实现
Tensorflow steps on the pit notes and records various errors and solutions
读论文-Cycle GAN
C语言—三子棋的实现