当前位置:网站首页>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;
}
}
};
边栏推荐
- 股票怎样在手机上开户安全吗 网上开户炒股安全吗
- Leetcode 718. Longest repeating subarray (violence enumeration, to be solved)
- 阿里云服务器的购买、基本配置、(xshell)远程连接、搭建环境
- ASP. Net core create MVC project upload file (buffer mode)
- [microservices] understanding microservices
- 股票开户有哪些优惠活动?手机开户安全么?
- Unity初学者肯定能用得上的50个小技巧
- go语言中的私聊功能处理
- Color matching and related issues
- Let agile return to its original source -- Some Thoughts on reading the way of agile neatness
猜你喜欢
通过两个stack来实现Queue
Unity初学者肯定能用得上的50个小技巧
xshell的安装、xftp的安装
Cvpr2022 stereo matching of asymmetric resolution images
The user adds a timer function in the handler () goroutine. If it times out, it will be kicked out
[machine learning] - Introduction to vernacular and explanation of terms
UnityEditor編輯器擴展-錶格功能
微信小程序自动生成打卡海报
Typera set title auto numbering
【LeetCode】1984. Minimum difference between highest and lowest of K scores
随机推荐
[try to hack] forward shell and reverse shell
论文学习——降雨场次划分方法对降雨控制率的影响分析
[microservice]eureka
股票怎样在手机上开户安全吗 网上开户炒股安全吗
Service discovery, storage engine and static website of go language
Introduction to message queuing
Let agile return to its original source -- Some Thoughts on reading the way of agile neatness
Unity4.6版本下载
【强基计划】数学与物理竞赛中的微积分部分视频
在线上买养老年金险正规安全吗?有没有保单?
买股票在手机上开户安全吗 网上开户炒股安全吗
Tensorrt笔记(七)Tensorrt使用问题整理
客户端实现client.go客户端类型定义连接
Nacos安装指南
xshell的安装、xftp的安装
ubuntu上安装mysql
阿里云服务器的购买、基本配置、(xshell)远程连接、搭建环境
用户在hander()goroutine,添加定时器功能,超时则强踢出
开放世界机甲游戏-Phantom Galaxies
[kotlin] keyword suspend learning of thread operation and async understanding