当前位置:网站首页>Leetcode skimming 4 Find the median of two positive arrays
Leetcode skimming 4 Find the median of two positive arrays
2022-06-26 23:39:00 【Charlesix59】
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
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/median-of-two-sorted-arrays
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
In fact, at the beginning, my idea was to combine these two vector Compose one and sort , Then take out the elements of the intermediate index , And return the corresponding median according to the parity bit . But both arrays are ordered , use sort Sort , Quick sort in the face of such a situation , Efficiency is not ideal . therefore , I didn't use this method for efficiency , Instead, we iterate through these two arrays , Increase the counter by one at a time , Until the counter reaches the middle value . This has better spatial complexity
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
bool is_odd=(nums1.size()+nums2.size())%2==0?false:true;
int mid1=0,mid2=0;
int p=0,q=0;
int tar=(nums1.size()+nums2.size())/2;
while(p+q<=tar){
mid1=mid2;
if(p>=nums1.size()){
mid2=nums2[q];
q++;
continue;
}
if(q>=nums2.size()){
mid2=nums1[p];
p++;
continue;
}
if(nums1[p]>nums2[q]){
mid2=nums2[q];
q++;
}
else{
mid2=nums1[p];
p++;
}
}
if(is_odd){
return mid2;}
else{
return (double)(mid1+mid2)/2;}
}
};
Later I saw a blog using STL Of marge Sort is to merge and sort two ordered arrays , So we have this method now , It is similar to my initial idea , This method has excellent time complexity
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
bool is_odd=(nums1.size()+nums2.size())%2==0?false:true;
int mid1=0,mid2=0;
int p=0,q=0;
vector<int> nums3;
nums3.resize(nums1.size()+nums2.size());
merge(nums1.begin(),nums1.end(),nums2.begin(),nums2.end(),nums3.begin());
mid2=nums3[nums3.size()/2];
if(is_odd){
return mid2;}
else{
mid1=nums3[nums3.size()/2-1];
return (double)(mid1+mid2)/2;
}
}
};
边栏推荐
- 论文学习——降雨场次划分方法对降雨控制率的影响分析
- Operator介绍
- Openpyxl module
- ASP. Net core create MVC project upload file (buffer mode)
- 【测试】最火的测试开发学习路线内容再次大更新,助力通关大厂测开
- Operator介紹
- 50 tips that unity beginners can definitely use
- Smartbi gives you a piece to play with Boston matrix
- Unityeditor Editor Extension - table function
- Nacos安装指南
猜你喜欢

My advanced learning notes of C language ----- keywords

go语言的服务发现、存储引擎、静态网站

【界面】pyqt5和Swin Transformer对人脸进行识别

【测试】最火的测试开发学习路线内容再次大更新,助力通关大厂测开

Thesis study -- Analysis of the influence of rainfall field division method on rainfall control rate

go语言的爬虫和中间件

入侵痕迹清理

UnityEditor編輯器擴展-錶格功能

BS-GX-016基于SSM实现教材管理系统

简单测试轻量级表达式计算器Flee
随机推荐
Thesis study -- Analysis of the influence of rainfall field division method on rainfall control rate
300 questions lesson 3 vector group
PHP code audit series (I) basis: methods, ideas and processes
ASP. Net core create MVC project upload file (buffer mode)
Leetcode 718. Longest repeating subarray (violence enumeration, to be solved)
Typera set title auto numbering
Installation of xshell and xftp
买股票在手机上开户安全吗 网上开户炒股安全吗
股票怎样在手机上开户安全吗 网上开户炒股安全吗
ASP.Net Core创建MVC项目上传文件(缓冲方式)
Implement the queue through two stacks
Operator介紹
The processing of private chat function in go language
Why don't I recommend going to sap training institution for training?
DAST 黑盒漏洞扫描器 第五篇:漏洞扫描引擎与服务能力
[try to hack] forward shell and reverse shell
我的c语言进阶学习笔记 ----- 关键字
Simple test lightweight expression calculator fly
[microservice]eureka
【老卫搞机】090期:键盘?主机?全功能键盘主机!