当前位置:网站首页>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
边栏推荐
- 7. Dependency injection
- Fudan University EMBA peer topic: always put the value of consumers in the most important position
- Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant
- Getting started with easyUI
- Hcip notes 12 days
- 第三章、数据类型和变量
- 多租户软件开发架构
- 【目标检测】YOLOv5跑通VisDrone数据集
- pgsql有没有好用的图形化管理工具?
- win10如何删除微软拼音输入法
猜你喜欢

Wu Enda logistic regression 2

Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project

Birui data joins Alibaba cloud polardb open source database community

【南京航空航天大学】考研初试复试资料分享

Text translation software - text batch translation converter free of charge

EasyUI drop-down box, add and put on and off shelves of products

Automatic reply of wechat official account development message
![[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?](/img/fd/54fcae2bc3f4313e9401c735ef74b8.png)
[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?

Briefly describe the implementation principle of redis cluster

Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again
随机推荐
Box selection screenshot shortcut key of win10
win10如何删除微软拼音输入法
[OBS] frame loss and frame priority before transmission
04.寻找两个正序数组的中位数
数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
pgsql有没有好用的图形化管理工具?
7.依赖注入
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part4): problem analysis and retrieval sentence generation combined with problem classification
使用Huggingface在矩池云快速加载预训练模型和数据集
从数字化到智能运维:有哪些价值,又有哪些挑战?
Sogou batch push software - Sogou batch push tool [2022 latest]
简述redis集群的实现原理
China's chip self-sufficiency rate has increased significantly, resulting in high foreign chip inventories and heavy losses. American chips can be said to have thrown themselves in the foot
After 20 years of agitation, the chip production capacity has started from zero to surpass that of the United States, which is another great achievement made in China
2022 latest Beijing Construction welder (construction special operation) simulation question bank and answer analysis
华泰vip账户证券开户安全吗
[cloud co creation] explore how gaussdb helps ICBC create core financial data
Multi tenant software development architecture
Frustrated Internet people desperately knock on the door of Web3
【目标检测】TPH-YOLOv5:基于transformer的改进yolov5的无人机目标检测