当前位置:网站首页>4. Find the median of two positive arrays
4. Find the median of two positive arrays
2022-07-02 02:53:00 【kfc++】
Given two sizes, they are m and n Positive order of ( From small to large ) Array nums1 and nums2. Please find and return the values of these two positive ordered arrays Median .
The time complexity of the algorithm should be O(log (m+n)) .
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
Tips :
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
source : Power button (LeetCode)
Force buckle code :
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
double ans;
int size1=nums1.size(), size2=nums2.size();
int size3=size1+size2;
int nums3[size3];
int i=0, j=0, k=0;
while(i<size1||j<size2)
{
if(i<size1&&j==0)
nums3[k++]=nums1[i++];
else
nums3[k++]=nums2[j++];
}
sort(nums3,nums3+size3);
if(size3%2)
{
ans=nums3[(size3-1)/2];
}
else
{
ans=(nums3[size3/2]+nums3[size3/2-1])/2.0;
}
return ans;
}
};边栏推荐
- Mongodb base de données non relationnelle
- Golang configure export goprivate to pull private library code
- Après le mariage
- Kibana操控ES
- JVM面试篇
- As a software testing engineer, will you choose the bank post? Laolao bank test post
- 【做题打卡】集成每日5题分享(第二期)
- LFM信号加噪、时频分析、滤波
- Deployment practice and problem solving of dash application development environment based on jupyter Lab
- [staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
猜你喜欢

高并发场景下缓存处理方案

Golang configure export goprivate to pull private library code

STM32__ 05 - PWM controlled DC motor

MVVM and MVC

LeetCode刷题(十)——顺序刷题46至50

Deployment practice and problem solving of dash application development environment based on jupyter Lab

AcWing 245. Can you answer these questions (line segment tree)

QT实现界面跳转

2022-2028 global encryption software industry research and trend analysis report

Jvm-01 (phased learning)
随机推荐
After marriage
Comparative analysis of MVC, MVP and MVVM, source code analysis
Mongodb non relational database
Face++ realizes face detection in the way of flow
【带你学c带你飞】3day第2章 用C语言编写程序(练习 2.3 计算分段函数)
[JVM] detailed description of the process of creating objects
Baohong industry | 6 financial management models at different stages of life
C # use system data. The split mixed mode assembly is generated for the "v2.0.50727" version of the runtime, and it cannot be loaded in the 4.0 runtime without configuring other information
Query word weight, search word weight calculation
Kibana controls es
es面试题
Learning notes of software testing -- theoretical knowledge of software testing
Unit · elementary C # learning notes
JVM面试篇
2022-2028 global military computer industry research and trend analysis report
2022-2028 global soft capsule manufacturing machine industry research and trend analysis report
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
Golang configure export goprivate to pull private library code
2022安全员-C证考试题及模拟考试
buu_ re_ crackMe