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

C++算法:寻找两个正序数组的中位数

题目

寻找两个正序数组的中位数
给定两个大小分别为 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.lryc.cn/news/186395.html

相关文章:

  • 2.1 关系数据结构及形式化定义
  • “揭秘淘宝店铺所有商品接口:一键获取海量热销宝贝信息!“
  • 跟着播客学英语-Why I use vim ? part two
  • 【网络通信三要素】TCP与UDP快速入门
  • k8s集群的简单搭建
  • 语义分割笔记(三):通过opencv对mask图片来画分割对象的外接椭圆
  • Nosql redis高可用和持久化
  • 软件工程(1、2;5~7小测参考答案)
  • 服务器存储面临的两大难题
  • Blind Signature盲签名与fabric区块链结合的应用
  • ueditor
  • 2023年台州市第三届网络安全技能大赛(MISC)—Black Mamba
  • 这道面试题工作中经常碰到,但 99% 的程序员都答不上来
  • Linux安装单机PostgreSQL15.4
  • 最新 SpringCloud微服务技术栈实战教程 微服务保护 分布式事务 课后练习等
  • Docker搭建MySQL8.0主从复制(一主一从)
  • 40V汽车级P沟道MOSFET SQ4401EY-T1_GE3 工作原理、特性参数、封装形式—节省PCB空间,更可靠
  • 记录在搭建Jenkins时,所遇到的坑,以及解决方案
  • 二极管“天马行空”的作用,你知道吗?
  • 鼎盛合:adc芯片的五种结构
  • CTF 全讲解:[SWPUCTF 2021 新生赛]Do_you_know_http
  • 物联网AI MicroPython传感器学习 之 4路电容式触摸开关
  • 头戴式耳机什么牌子最好?头戴式耳机推荐性价比高
  • 第 366 场周赛 LeetCode 周赛题解
  • Linux: tcpdump抓包示例
  • seafile server10.0.1 onlyoffice
  • 商城系统选型:Java商城系统还是PHP商城系统好?
  • 【多线程进阶】线程安全的集合类
  • 016 Spring Boot + Vue 图书管理系统
  • C语言中volatile/register/const/static/extern/auto关键字的作用