当前位置:网站首页>04. Find the median of two positive arrays
04. Find the median of two positive arrays
2022-07-25 17:10:00 【User 5573316】
#04. Find the median of two positive arrays
difficulty : difficult
Given two sizes m and n Positive order of ( From small to large ) Array nums1 and nums2. Please find and return the median of these two positive arrays .
Advanced : You can design a time complexity of O(log (m+n)) Does the algorithm solve this problem ?
Example 1:
Input :nums1 = [1,3], nums2 = [2] Output :2.00000 explain : Merge array = [1,2,3] , Median 2 Example 2:
Input :nums1 = [1,2], nums2 = [3,4] Output :2.50000 explain : Merge array = [1,2,3,4] , Median (2 + 3) / 2 = 2.5 Example 3:
Input :nums1 = [0,0], nums2 = [0,0] Output :0.00000 Example 4:
Input :nums1 = [], nums2 = [1] Output :1.00000 Example 5:
Input :nums1 = [2], nums2 = [] Output :2.00000
Tips :
nums1.length == m nums2.length == n 0 <= m <= 1000 0 <= n <= 1000 1 <= m + n <= 2000 -106 <= nums1[i], nums2[i] <= 106
# violence
# Ideas
First, combine the two arrays into an array , Traversal
# Code
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length, n = nums2.length;
if (m == 0) {
return getMid(nums2);
}
if (n == 0) {
return getMid(nums1);
}
int[] temp = new int[m + n];
int i = 0, j = 0, count = 0;
while (count != (m + n)) {
if (i == m) {
while (j != n) {
temp[count++] = nums2[j++];
}
break;
}
if (j == n) {
while (i != m) {
temp[count++] = nums1[i++];
}
break;
}
if (nums1[i] < nums2[j]) {
temp[count++] = nums1[i++];
} else {
temp[count++] = nums2[j++];
}
}
return getMid(temp);
}
public double getMid(int[] nums) {
if (nums.length % 2 == 0) {
return (nums[nums.length / 2] + nums[nums.length / 2 - 1]) / 2.0;
} else {
return nums[nums.length / 2];
}
}
}
# Dichotomy
To be added
边栏推荐
- Replicate swin on Huawei ascend910_ transformer
- Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
- ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
- Summary of 80 domestic database operation documents (including tidb, Damon, opengauss, etc.)
- 搜狗批量推送软件-搜狗批量推送工具【2022最新】
- [target detection] yolov5 Runtong visdrone data set
- Getting started with easyUI
- 为什么 4EVERLAND 是 Web 3.0 的最佳云计算平台
- 基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
- 月薪1万在中国是什么水平?答案揭露残酷的收入真相
猜你喜欢

Hcip notes 11 days

在华为昇腾Ascend910上复现swin_transformer

How to install govendor and open a project

7.依赖注入

Hcip notes 12 days

EasyUI modification and DataGrid dialog form control use

7. Dependency injection

win10如何删除微软拼音输入法

WPF implements user avatar selector
![[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part3): rule-based problem classification](/img/4c/aeebbc9698f8d5c23ed6473c9aca34.png)
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part3): rule-based problem classification
随机推荐
Use huggingface to quickly load pre training models and datasets in moment pool cloud
异常处理机制专题1
HCIP笔记十二天
Hcip notes 11 days
Redis cluster deployment based on redis6.2.4
Rosen's QT journey 99 QML table control tableview
What are the free low code development platforms?
Rainbow plug-in extension: monitor MySQL based on MySQL exporter
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification
第三章、数据类型和变量
Talk about how to use redis to realize distributed locks?
Roson的Qt之旅#99 QML表格控件-TableView
[target detection] yolov5 Runtong visdrone data set
Rebudget汇报PPT
华泰vip账户证券开户安全吗
In the eyes of 100 users, there are 100 QQS
Enterprise live broadcast: witness focused products, praise and embrace ecology
stm32F407------SPI
Using rank to discuss the solution of linear equations / the positional relationship of three planes
在华为昇腾Ascend910上复现swin_transformer