当前位置:网站首页>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;
}
};
边栏推荐
- After marriage
- 实现一个自定义布局的扫码功能
- 【liuyubobobo-玩转Leetcode算法面试】【00】课程概述
- [learn C and fly] 4day Chapter 2 program in C language (exercise 2.5 generate power table and factorial table
- 2022 safety officer-c certificate examination questions and mock examination
- [learn C and fly] 2day Chapter 8 pointer (practice 8.1 password unlocking)
- Comparative analysis of MVC, MVP and MVVM, source code analysis
- How to run oddish successfully from 0?
- Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
- Render header usage of El table
猜你喜欢
Batch detect whether there is CDN in URL - high accuracy
LFM signal denoising, time-frequency analysis, filtering
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
Jointly developed by nailing, the exclusive functions of glory tablet V7 series were officially launched
About DNS
2022-2028 global wood vacuum coating machine industry research and trend analysis report
【带你学c带你飞】2day 第8章 指针(练习8.1 密码开锁)
How to develop digital collections? How to develop your own digital collections
Start a business
Connected block template and variants (4 questions in total)
随机推荐
使用 useDeferredValue 进行异步渲染
Duplicate keys detected: ‘0‘. This may cause an update error. found in
Build a modern data architecture on the cloud with Amazon AppFlow, Amazon lake formation and Amazon redshift
[learn C and fly] 1day Chapter 2 (exercise 2.2 find the temperature of Fahrenheit corresponding to 100 ° f)
[staff] restore mark (Introduction to the use of restore mark | example analysis of Metaphone mark and restore mark)
[staff] pitch representation (treble clef | C3 60 ~ B3 71 pitch representation | C4 72 pitch representation | C5 84 pitch representation)
[staff] diacritical mark (ascending sign | descending sign B | double ascending sign x | double descending sign BB)
As a software testing engineer, will you choose the bank post? Laolao bank test post
高并发场景下缓存处理方案
Face++ realizes face detection in the way of flow
Provincial election + noi Part IV graph theory
Oracle creates a user with read-only permission in four simple steps
设置状态栏颜色
Learning notes of software testing -- theoretical knowledge of software testing
JVM面试篇
Multi threaded query, double efficiency
QT使用sqllite
Addition without addition, subtraction, multiplication and division (simple difficulty)
How does proxy IP participate in the direct battle between web crawlers and anti crawlers
Query word weight, search word weight calculation