当前位置:网站首页>【【【递归】】】
【【【递归】】】
2022-07-24 05:15:00 【rejudge】
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int tot=nums1.size()+nums2.size();
if(tot%2==0){
int left=find(nums1,0,nums2,0,tot/2);
int right=find(nums1,0,nums2,0,tot/2+1);
return (left+right)/2.0; //注意不要取整
}else{
return find(nums1,0,nums2,0,tot/2+1);
}
}
int find(vector<int>& nums1,int i,vector<int>& nums2,int j,int k){
//第一个数组短
if(nums1.size()-i>nums2.size()-j)
return find(nums2,j, nums1,i,k);
//边界1:返回两个数组最小值
if(k==1){
//nums1可能为空
if(nums1.size()==i) return nums2[j];
else return min(nums1[i],nums2[j]);
}
//边界2:第一个数组为空 反回第二个数组第k个数
if(nums1.size()==i) return nums2[j+k-1];
//递归部份
//vector数组中size()返回值是size_type,而size_type的类型是unsigned int,min接受int型参数,需要强转
int si=min((int)nums1.size(),i+k/2); //第k/2个元素的后一个元素,可能长度不够
int sj=j+k-k/2;
if(nums1[si-1]>nums2[sj-1])
//nums2前段不再考虑
return find(nums1,i,nums2,sj,k-(sj-j));
else
//nums1前段不再考虑
return find(nums1,si,nums2,j,k-(si-i));
}
};
边栏推荐
- Do you want to have a robot that can make cartoon avatars in three steps?
- SSH service
- JMeter FAQs
- MySQL连接
- [advanced mathematics] the difference between differentiable and differentiable functions
- 支撑复杂的模型群监控、实时告警等t4 文件系统。e
- Industry relevance of stock price trend
- Teach you how to weld CAD design board bottom (for beginners) graphic tutorial
- OSS文件上传
- 浅谈不可转让的声誉积分NFT SBTs面临的困境
猜你喜欢

Source code compilation!!

PostgreSQL: run PostgreSQL + pgadmin 4 in docker

OSS文件上传

【深度学习】(三)图像分类

HCIA NAT experiment

Beginners' preparation for the Blue Bridge Cup (University Programming learning history records, topic ideas for students who need to prepare for the Blue Bridge Cup)

Image to image translation with conditional advantageous networks paper notes

Blue Bridge Cup 31 day sprint 21 day (C language)

PPPoE gateway simulation environment setup

How to set up an internal wiki for your enterprise?
随机推荐
利用a*启发式搜索解决迷宫寻路问题
Career planning route
Performance test process
Reading excerpts from Liu run's "bottom logic"
PXE efficient batch network installation
连接数%的准确率。现在拟需求。企业在数足以
Drools development decision table
一文带你深入浅出C字符串函数和内存函数
FTP file transfer protocol
I'm interested in reading efficient reading - the most cost-effective self investment
[basic 7] - exceptions, capture and customize exceptions
JMeter record the BeanShell written into excel instance caused by an automatic data generation
NFS shared services
generator生成器,只生成两个方法
IDEA:SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.
Context encoders: feature learning by painting paper notes
浅谈不可转让的声誉积分NFT SBTs面临的困境
Hcip day 3 - mGRE experiment
Tips for using the built-in variable props of BeanShell
Wang Qing, director of cloud infrastructure software research and development of Intel (China): Intel's technology development and prospects in cloud native