当前位置:网站首页>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;
}
};边栏推荐
- QT实现界面跳转
- [staff] pitch representation (bass clef | C1 36 note pitch representation | C2 48 note pitch representation | C3 60 note pitch representation)
- [staff] restore mark (Introduction to the use of restore mark | example analysis of Metaphone mark and restore mark)
- Ten minutes will take you in-depth understanding of multithreading - multithreaded teamwork: synchronous control
- [learn C and fly] day 5 chapter 2 program in C language (Exercise 2)
- Connected block template and variants (4 questions in total)
- How to develop digital collections? How to develop your own digital collections
- [staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
- Coordinatorlayout + tablayout + viewpager2 (there is another recyclerview nested inside), and the sliding conflict of recyclerview is solved
- 2022-2028 global human internal visualization system industry research and trend analysis report
猜你喜欢

Additional: information desensitization;

Is bone conduction earphone better than traditional earphones? The sound production principle of bone conduction earphones is popular science

Render header usage of El table

query词权重, 搜索词权重计算

As a software testing engineer, will you choose the bank post? Laolao bank test post
![[road of system analyst] collection of wrong topics in enterprise informatization chapter](/img/c4/0bb4823ca85c440b4e0587a91b2585.jpg)
[road of system analyst] collection of wrong topics in enterprise informatization chapter
![[liuyubobobo play with leetcode algorithm interview] [00] Course Overview](/img/1c/c8cab92c74b6658c3ef608c5255f1f.png)
[liuyubobobo play with leetcode algorithm interview] [00] Course Overview

浅谈线程池相关配置

MVVM and MVC

Leetcode question brushing (10) - sequential question brushing 46 to 50
随机推荐
JVM interview
Which brand of sports headset is better? Bluetooth headset suitable for sports
Questions d'entrevue
[learn C and fly] 3day Chapter 2 program in C language (exercise 2.3 calculate piecewise functions)
2022安全员-C证考试题及模拟考试
【带你学c带你飞】day 5 第2章 用C语言编写程序(习题2)
MVVM and MVC
What is the function of the headphone driver
Deployment practice and problem solving of dash application development environment based on jupyter Lab
結婚後
【带你学c带你飞】1day 第2章 (练习2.2 求华氏温度 100°F 对应的摄氏温度
Render header usage of El table
Baohong industry | four basic knowledge necessary for personal finance
2022-2028 global manual dental cleaning equipment industry research and trend analysis report
Websocket + spingboot realize code scanning login
Pat a-1165 block reversing (25 points)
AcWing 245. Can you answer these questions (line segment tree)
New programmer magazine | Li Penghui talks about open source cloud native message flow system
The video number will not be allowed to be put on the shelves of "0 yuan goods" in the live broadcasting room?
Connected block template and variants (4 questions in total)