当前位置: 首页 > news >正文

东营做营销型网站河北网站推广

东营做营销型网站,河北网站推广,免费手机网站制作,app定制版题目 寻找两个正序数组的中位数 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。 算法的时间复杂度应该为 O(log (mn)) 。 示例 1: 输入:nums1 [1,3], nums2 [2] 输…

题目

寻找两个正序数组的中位数
给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
算法的时间复杂度应该为 O(log (m+n)) 。
示例 1:
输入:nums1 = [1,3], nums2 = [2]
输出:2.00000
解释:合并数组 = [1,2,3] ,中位数 2
示例 2:
输入:nums1 = [1,2], nums2 = [3,4]
输出:2.50000
解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5

2023年3月13号的解法

class Solution {
public:
double findMedianSortedArrays(vector& nums1, vector& nums2) {
const int Len = nums1.size() + nums2.size();
const int iAvg1 = Rec(nums1.data(), nums1.data() + nums1.size(), nums2.data(), nums2.data() + nums2.size(), (Len - 1) / 2);
if (Len & 1)
{
return iAvg1;
}
const int iAvg2 = Rec(nums1.data(), nums1.data() + nums1.size(), nums2.data(), nums2.data() + nums2.size(), (Len - 1) / 2+1);
return (iAvg1 + iAvg2) / 2.0;
}
int Rec(int* b1, int* e1, int* b2, int* e2, int iFindIndex)
{
if (b1 == e1)
{
return b2[iFindIndex];
}
if (b2 == e2)
{
return b1[iFindIndex];
}
if (0 == iFindIndex)
{
return min(*b1, *b2);
}
int k = (iFindIndex + 1) / 2;
const int index1 = min(k - 1,(int)( e1 - b1)-1);
const int index2 = min(k - 1, (int)(e2 - b2 )- 1);
if (b1[index1] < b2[index2])
{
return Rec(b1 + index1 + 1, e1, b2, e2, iFindIndex - index1 - 1);
}
return Rec(b1, e1, b2 + index2 + 1, e2, iFindIndex - index2 - 1);
}
};

2023年8月6号的解法

class Solution {
public:
double findMedianSortedArrays(vector& nums1, vector& nums2) {
m_c = nums1.size() + nums2.size();
m_iHalf = m_c / 2;
int left = 0, r = min(m_iHalf, (int)nums1.size()) + 1;//左闭右开
while (r > left + 1)
{
const auto mid = left + (r - left) / 2;
const int leftLen2 = m_iHalf - mid;
const int iRet = Cmp(mid, leftLen2, nums1, nums2);
if (0 == iRet)
{
break;
}
else if (iRet < 0)
{
r = mid;
}
else
{
left = mid;
}
}
if (m_dRet < 0 )
{
Cmp(left,m_iHalf-left,nums1,nums2);
}
return m_dRet;
}
int Cmp(int leftLen1, int leftLen2, const vector& nums1, const vector& nums2)
{
if (leftLen2 > nums2.size())
{
return 1;
}
int iLeftMax = INT_MIN;
if (leftLen1 > 0)
{
iLeftMax = max(iLeftMax, nums1[leftLen1 - 1]);
}
if (leftLen2 > 0)
{
iLeftMax = max(iLeftMax, nums2[leftLen2 - 1]);
}
int iRightMin = INT_MAX;
if (leftLen1 < nums1.size())
{
iRightMin = min(iRightMin, nums1[leftLen1]);
}
if (leftLen2 < nums2.size())
{
iRightMin = min(iRightMin, nums2[leftLen2]);
}
if (iLeftMax <= iRightMin)
{
if (1 & m_c)
{
m_dRet = iRightMin;
}
else
{
m_dRet = (iLeftMax + iRightMin) / 2.0;
}
return 0;
}
if ((leftLen1 > 0) && (nums1[leftLen1 - 1] > iRightMin))
{
return-1;
}
return 1;
}
double m_dRet=-1;
int m_c;
int m_iHalf;
};

其它

视频课程

如果你觉得复杂,想从简单的算法开始,可以学习我的视频课程。
https://edu.csdn.net/course/detail/38771
我的其它课程
https://edu.csdn.net/lecturer/6176

测试环境

win7 VS2019 C++17

相关下载

doc版文档,排版好
https://download.csdn.net/download/he_zhidan/88348653

http://www.yayakq.cn/news/178873/

相关文章:

  • 一个网站可以有几个关键词网站建设合同的主要内容
  • 花生壳域名可以做网站域名吗搜索引擎优化工作主要做好哪些方面
  • 免费注册网站域名免费自助建站网站建设免费信息发布
  • 做自己网站做站长wordpress在线
  • 电子商务有限公司网站内网电脑做网站服务器
  • 中国建设银行肃宁支行网站杭州h5建站
  • 如何仿别人网站的莫板wordpress子站搭建
  • 成都做网站的张雪峰谈广告学专业
  • 有没有兼职做网站的wordpress取订阅数据
  • 网站美工工作步骤是什么东莞网站建设 牛魔网
  • 南昌哪里有建设网站的建设厅科技中心网站
  • 重庆企业网站seo提供建立网站服务的公司
  • 互联网站备案信息查询上海购物网站建设
  • 免手机微网站搜索点击软件
  • 贺州网站建设公司wordpress 瀑布流ajax
  • 怎么建立织梦网站网站多服务器建设
  • 外贸网站官网怎么做做网站公司哪好
  • 咨询型网站郑州营销型网站建设哪家好
  • 常州网站的优化大连领超科技网站建设有限公司
  • 无锡锡山区建设局网站thinkphp做网站后台
  • 拼多多网站建设方案php 网站开发模式
  • 南京建设监理协会网站打不开北京海淀网站建设公司
  • 做暧免费观看网站站长之家工具
  • 怒江州住房和城乡建设局网站查域名解析
  • 网站制作协议触屏网站meta标签
  • 建网站需要多少钱和什么条件中国建设招标工程网站
  • 网站编辑做图片用什么网站 功能需求
  • 阿里云网站建设方案书一定要嘛已备案域名买卖
  • 做是么网站友情链接多少钱一个
  • 怎么看一个网站的cms弹幕网站是什么技术做的