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

北京网站制作培训班重庆江北营销型网站建设公司推荐

北京网站制作培训班,重庆江北营销型网站建设公司推荐,智威汤逊广告公司,wordpress转dede寻找两个正序数组的中位数 仅供学习 题目 算法时间复杂度 二分查找算法,时间复杂度为 O(log(min(m, n))),其中 m 和 n 分别是两个数组的长度。 子函数 查找两个数字的最大值 int max(int a, int b) {return a > b ? a : b; }查找两个数字的最小…

寻找两个正序数组的中位数

仅供学习

题目

在这里插入图片描述


算法时间复杂度

二分查找算法,时间复杂度为 O(log(min(m, n))),其中 m 和 n 分别是两个数组的长度。

子函数

查找两个数字的最大值

int max(int a, int b) {return a > b ? a : b;
}

查找两个数字的最小值

int min(int a, int b) {return a < b ? a : b;
}

findMedianSortedArrays

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {// Ensure nums1 is the smaller arrayif (nums1Size > nums2Size) {return findMedianSortedArrays(nums2, nums2Size, nums1, nums1Size);}int x = nums1Size;int y = nums2Size;int low = 0;int high = x;while (low <= high) {int partitionX = (low + high) / 2;int partitionY = (x + y + 1) / 2 - partitionX;int maxX = (partitionX == 0) ? INT_MIN : nums1[partitionX - 1];int minX = (partitionX == x) ? INT_MAX : nums1[partitionX];int maxY = (partitionY == 0) ? INT_MIN : nums2[partitionY - 1];int minY = (partitionY == y) ? INT_MAX : nums2[partitionY];if (maxX <= minY && maxY <= minX) {// We have partitioned array at the correct place// Now we get max of left elements and min of right elements to get the median in case of even length combined array sizeif ((x + y) % 2 == 0) {return ((double)max(maxX, maxY) + min(minX, minY)) / 2;} else {return (double)max(maxX, maxY);}} else if (maxX > minY) { // we are too far on the right side for partitionX. Go on left side.high = partitionX - 1;} else { // we are too far on the left side for partitionX. Go on right side.low = partitionX + 1;}}// If we reach here, it means the arrays are not sortedfprintf(stderr, "Input arrays are not sorted or there is some other error.\n");return -1;
}

说明

  • 该代码实现了一个查找两个正序数组中位数的算法,使用了二分查找法来优化时间复杂度。
  • findMedianSortedArrays 函数首先确保第一个数组(nums1)是较小的一个,这样可以减小搜索范围。
  • 在 while 循环中,通过二分查找确定两个数组的分割点,使得分割后的左半部分和右半部分元素数量接近。
  • 根据分割点计算最大左边元素和最小右边元素,进而确定中位数。
  • 主函数通过示例数据验证了算法的正确性。
http://www.yayakq.cn/news/646107/

相关文章:

  • 网站开发哪种语言最好公司建设网站的报告书
  • 专业做网站系统wordpress熊掌号展示
  • 网站链接导出免费下载中国移动app
  • 网站右键屏蔽小程序是一种后端微服务
  • 高端网站建设wordpress微电影模板下载
  • 青海 网站开发 app软件开发文档的需求分析
  • 深圳工信部网站免费制作企业宣传视频app
  • 网站优化外包推荐受欢迎的企业网站建设
  • 手机网站制作要求标准做ppt哪个网站好
  • 网站建设的现状与趋势论文推广流程
  • 网站公司建设外贸网站建设方案
  • 福州网站制作系统二维码 wordpress
  • 网站开发兼容问题高端网站设计优化建站
  • 编程 网站建设科迪兔网站建设
  • 有没有专门招代理的网站推广赚钱
  • 网站模板下载网站有哪些内容广州天河区酒店
  • 建立手机网站网页制作导航栏
  • CDN 网站是否需要重新备案网站怎么建立支付平台
  • 做网站域名解析在线培训网站
  • 果业局网站建设上海代理工商注册公司
  • 郑州做网站那家好wordpress _e函数
  • 许昌市做网站汉狮网络产品宣传片
  • 怎么自学网站建设4399影视在线观看免费高清
  • 长沙网站建设 网站设计wordpress 删除小工具栏
  • 丹东谁家做网站金华网站推广
  • 做抽奖网站用什么cms电子商务网站建设教程
  • 棋牌游戏网站开发龙岩网红
  • 公司做网站流程重庆招聘网站有哪些
  • 网站程序前台怎么创建自己的官网
  • logo商标设计网站电子商务营销