题目描述:

给定两个大小为 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++的更多相关文章

  1. 求两个排序数组的交集和并集----时间复杂度O(n+m)

    问题: 给你两个排序的数组,求两个数组的交集. 比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么交集就是 3 5,n是a数组大小,m是b数组大小. 思路: (1)从b数组遍历取 ...

  2. 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 ...

  3. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  4. Leetcode4--->求两个排序数组的中位数

    题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...

  5. LeetCode(4):两个排序数组的中位数

    Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...

  6. 求两个有序数组的中位数或者第k小元素

    问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...

  7. LeetCode4. 两个排序数组的中位数

    4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...

  8. LeetCode-4. 两个排序数组的中位数(详解)

    链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...

  9. JavaScript实现获取两个排序数组的中位数算法示例

    本文实例讲述了JavaScript排序代码实现获取两个排序数组的中位数算法.分享给大家供大家参考,具体如下: 题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个 ...

随机推荐

  1. div设置overflow-scroll滚动之后,jq获取其子元素的offset.top出现问题。

    先上个图: 布局很简单,左右超过屏幕的部分自行滚动. 1. html <div class="ce-container"> <div class="ce ...

  2. 将本地项目上传至github

    1.新建仓库:用于存放要上传的项目(尽量不要添加README). 2.找到要上传的文件夹A,右键点击git bash here,打开git界面 3.在命令行中,输入“git init”,使文件夹A加入 ...

  3. JVM 虚拟机笔记

    GC算法 标记-清除算法复制算法标记-整理算法分代收集 新生代--[15次GC]-->老年代新生代:复制算法老年代: 标记-清除算法或标记-整理算法 GC策略采用的何种算法引用计数算法根搜索算法 ...

  4. 腾讯云部署javaWeb项目之一应用服务器

    1.登录腾讯云,点击登录选择浏览器登录.输入用户名 按回车键 然后输入 密码. 2.安装java环境,直接命令:yum -y install java-1.8.0-openjdk java-1.8.0 ...

  5. 使用java的wsimport.exe生成wsdl的客户端代码【转】

    使用java的wsimport.exe工具生成wsdl的客户端代码 wsimport -keep -d d:\ -s d:\src -p com.map -verbose http://192.168 ...

  6. bzoj千题计划318:bzoj1396: 识别子串(后缀自动机 + 线段树)

    https://www.lydsy.com/JudgeOnline/problem.php?id=1396 后缀自动机的parent树上,如果不是叶子节点,那么至少有两个子节点 而一个状态所代表子串的 ...

  7. YouCompleteMe/third_party/ycmd/third_party/cregex" does not appear to contain CMakeLists.txt.

    rm -rf YouCompleteMe/third_party/ycmd/third_party/cregex git submodule update --init --recursive  at ...

  8. ArcGis Python脚本——根据字段内容拆分要素类(shp)为多个

    其实,这就是批量执行了ArcToolbox 分析工具-筛选 功能. 先上代码,后做解说: # in_feature:待拆分要素类 # out_folderpath:输出路径,注意最后加“/”以与字段名 ...

  9. ava.io.InputStream & java.io.FileInputStream

    java.io.InputStream & java.io.FileInputStream java.io.InputStream,这个抽象类是表示字节输入流的超类,这个抽象类的共性的方法有: ...

  10. python练习实例2--------构造数据类型

    有如下数据类型 cars = ['鲁A32444', '鲁B12333', '京B8989M', '黑C49678', '黑C46555', '沪B25041', '黑C34567'] locatio ...