求两个排序数组中位数 C++
题目描述:
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。
请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。
你可以假设 nums1 和 nums2 不同时为空。
示例 1:
- nums1 = [1, 3]
- nums2 = [2]
- 中位数是 2.0
示例 2:
- nums1 = [1, 2]
- nums2 = [3, 4]
- 中位数是 (2 + 3)/2 = 2.5
一个小技巧:
假设A、B数组分别有m,n个元素,我们找(m+n+1)/2和(m+n+2)/2这两个数,然后求其平均值,无论A、B两数组元素总数是奇数或者偶数,都能求得。
思路:
- class Solution {
- public:
- double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
- int m = nums1.size();
- int n = nums2.size();
- int left = (m + n + 1) / 2;
- int right = (m + n + 2) / 2;
- return (findKth(nums1, 0, nums2, 0, left) + findKth(nums1, 0, nums2, 0, right)) / 2.0; //这一步可以省去奇偶分别讨论
- }
- int findKth(vector<int>& nums1, int i, vector<int>& nums2, int j, int k) {
- if (i >= nums1.size()) return nums2[j + k - 1];
- if (j >= nums2.size()) return nums1[i + k - 1];
- if (k == 1) return min(nums1[i], nums2[j]);
- int midVal1 = (i + k / 2 - 1 < nums1.size()) ? nums1[i + k / 2 - 1] : INT_MAX;
- int midVal2 = (j + k / 2 - 1 < nums2.size()) ? nums2[j + k / 2 - 1] : INT_MAX;
- if (midVal1 < midVal2) {
- return findKth(nums1, i + k / 2, nums2, j, k - k / 2);
- }
- else {
- return findKth(nums1, i, nums2, j + k / 2, k - k / 2);
- }
- }
- };
求两个排序数组中位数 C++的更多相关文章
- 求两个排序数组的交集和并集----时间复杂度O(n+m)
问题: 给你两个排序的数组,求两个数组的交集. 比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么交集就是 3 5,n是a数组大小,m是b数组大小. 思路: (1)从b数组遍历取 ...
- LeetCode:4_Median of Two Sorted Arrays | 求两个排序数组的中位数 | Hard
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- Leetcode4--->求两个排序数组的中位数
题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...
- LeetCode(4):两个排序数组的中位数
Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...
- 求两个有序数组的中位数或者第k小元素
问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...
- LeetCode4. 两个排序数组的中位数
4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...
- LeetCode-4. 两个排序数组的中位数(详解)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...
- JavaScript实现获取两个排序数组的中位数算法示例
本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...
随机推荐
- org.hibernate.MappingException: class com.itheima.domain.Customer.java not found while looking for property: cust_id at org.hibernate.internal.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.
我这次异常的出现时,没有配置逐渐生成策略.
- mac/Linux源码安装TensorFlow
因为用pip命令直接下载安装会链接到google,导致打不开,比如使用pip install tensorflow碰到如下的问题.因此在本文中,主要介绍了如何通过源码进行TensorFlow的安装 $ ...
- 使用CMD 命令创建指定大小的文件
在做资源更新的时候要做 磁盘空间不足的测试,于是想创建一个文件塞满硬盘,搜索到可以用命令来创建. fsutil file createnew null.zip 524288000
- 关于交叉熵(cross entropy),你了解哪些
二分~多分~Softmax~理预 一.简介 在二分类问题中,你可以根据神经网络节点的输出,通过一个激活函数如Sigmoid,将其转换为属于某一类的概率,为了给出具体的分类结果,你可以取0.5作为阈值, ...
- python数据结构总结
一.列表 1.列表脚本操作符: (1)扩增的操作符: “+”:用于组合列表:如[1,2,3]+[4,5,6]==>[1,2,3,4,5,6] "*":重复;如[2,3]*2= ...
- 9.组合模式(Composite Pattern)
动机(Motivate): 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素 ...
- .net多站点通过StateServer实现session共享
先在所有要共享站点web.config的<system.web>结点下加 <!--session的mode=StateServer--><sessionState coo ...
- 解决Lost connection to MySQL server during query错误方法
昨天使用Navicat for MySQL导入MySQL数据库的时候,出现了一个严重的错误,Lost connection to MySQL server during query,字面意思就是在查询 ...
- ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(模拟+线段树)
https://nanti.jisuanke.com/t/30996 题意 每天增加m个灯泡,n个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了. ...
- Golang入门教程(十五)指针
什么是指针? 指针是一个变量,用于存储另一个变量的内存地址. 在上面的例子中,变量b的值是156,存储在内存地址0x1040a124. 变量a包含b的地址. 可以说现在a指向b. 声明指针 指向类型 ...