当前位置:网站首页>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;
}
}
};
边栏推荐
- BS-GX-016基于SSM实现教材管理系统
- Electronic Society C language level 1 31. Calculate line segment length
- 300题 第三讲 向量组
- 不同的子序列问题I
- The client implements client Go client type definition connection
- 消息队列简介
- DAST black box vulnerability scanner part 5: vulnerability scanning engine and service capability
- [微服務]認識微服務
- Nacos installation guide
- 想买股票请问在券商公司的哪里开户佣金低更安全
猜你喜欢
![[machine learning] - Introduction to vernacular and explanation of terms](/img/4c/e18fe52a71444c2ca08167ead9f28f.jpg)
[machine learning] - Introduction to vernacular and explanation of terms

ASP.Net Core创建MVC项目上传文件(缓冲方式)

Alibaba cloud server purchase, basic configuration, (xshell) remote connection and environment building
![[test] the content of the hottest test development learning route has been updated again to help pass the customs and open the test of large factories](/img/ee/b7cb528b79036896da781b73620758.jpg)
[test] the content of the hottest test development learning route has been updated again to help pass the customs and open the test of large factories

Common techniques of email attachment phishing

Crawler and Middleware of go language

Outside the code: writing is the best way to force growth

12 color ring three primary colors
![[微服务]Eureka](/img/60/e5fa18d004190d4dadebfb16b93550.png)
[微服务]Eureka

Introduction to software engineering -- Chapter 4 -- formal description technology
随机推荐
Why does EDR need defense in depth to combat ransomware?
手机炒股靠谱吗 网上开户炒股安全吗
Is it reliable to open an account on a stock trading mobile phone? Is it safe to open an account online and speculate in stocks
论文学习——降雨场次划分方法对降雨控制率的影响分析
Would you like to buy stocks? Where do you open an account in a securities company? The Commission is lower and safer
Let agile return to its original source -- Some Thoughts on reading the way of agile neatness
leetcode 1143. Longest common subsequence (medium)
股票开户有哪些优惠活动?手机开户安全么?
手机上可以开户炒股吗 网上开户炒股安全吗
Common techniques of email attachment phishing
Electronic Society C language level 1 29, alignment output
微信小程序自动生成打卡海报
CVE-2022-30190 Follina Office RCE分析【附自定义word模板POC】
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
6.24 学习内容
在线上买养老年金险正规安全吗?有没有保单?
Why don't I recommend going to sap training institution for training?
From bitmap to bloom filter, C # implementation
【测试】最火的测试开发学习路线内容再次大更新,助力通关大厂测开
[微服务]Nacos