当前位置:网站首页>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;
}
}
};
边栏推荐
- Where is it safer to open an account to buy funds
- 为什么EDR需要深度防御来打击勒索软件?
- 在线上买养老年金险正规安全吗?有没有保单?
- BS-GX-016基于SSM实现教材管理系统
- Electronic Society C language level 1 29, alignment output
- Pinpoint attackers with burp
- Is it reliable to open an account for stock trading on the mobile phone? Is it safe to open an account for stock trading on the Internet
- 手机上炒股开户可靠吗 网上开户炒股安全吗
- 能在手机上开户炒股吗 网上开户炒股安全吗
- Is the low commission free account opening channel safe?
猜你喜欢

Introduction to software engineering -- Chapter 4 -- formal description technology

On cap theorem in distributed system development technology

CVPR2022-不对称分辨率图像的立体匹配

开放世界机甲游戏-Phantom Galaxies

DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability

软件工程导论——第四章——形式化说明技术

运筹说 第66期|贝尔曼也有“演讲恐惧症”?
![[microservice]eureka](/img/60/e5fa18d004190d4dadebfb16b93550.png)
[microservice]eureka

Service discovery, storage engine and static website of go language

UnityEditor编辑器扩展-表格功能
随机推荐
go中的微服务和容器编排
【Try to Hack】正向shell和反向shell
Would you like to buy stocks? Where do you open an account in a securities company? The Commission is lower and safer
Leetcode 718. 最长重复子数组(暴力枚举,待解决)
国内外最好的12款项目管理系统优劣势分析
Smartbi gives you a piece to play with Boston matrix
Development and learning route of golang language
Leetcode 718. Longest repeating subarray (violence enumeration, to be solved)
UnityEditor编辑器扩展-表格功能
leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)
Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad
The user adds a timer function in the handler () goroutine. If it times out, it will be kicked out
入侵痕迹清理
电子协会 C语言 1级 30 、 等差数列末项计算
Electronic Society C language level 1 29, alignment output
Unityeditor Editor Extension - table function
Common techniques of email attachment phishing
6.24 learning content
12色彩环三原色
go语言的服务发现、存储引擎、静态网站