给定两个大小为 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

bool cmp1(int x, int y)
{
return x < y;
} class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int len1 = nums1.size();
int len2 = nums2.size();
vector<int> v;
for(int i = 0; i < len1; i++)
v.push_back(nums1[i]);
for(int i = 0; i < len2; i++)
v.push_back(nums2[i]);
sort(v.begin(), v.end(), cmp1);
int len3 = v.size();
if((len3 & 1) == 1)
{
return v[len3 / 2];
}
else
{
return (double)(v[len3 / 2 - 1] + v[len3 / 2]) / 2;
}
}
};

Leetcode4.Median of Two Sorted Arrays两个排序数组的中位数的更多相关文章

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

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

  2. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  3. [LeetCode] 4. Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  4. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  5. 004 Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two ...

  6. 【medium】4. Median of Two Sorted Arrays 两个有序数组中第k小的数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  7. 【LeetCode】4.Median of Two Sorted Arrays 两个有序数组中位数

    题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  8. 4. Median of Two Sorted Arrays(2个有序数组的中位数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  9. Median of Two Sorted 求两个有序数组的中位数

    中位数是把一个数的集合划分为两部分,每部分包含的数字个数相同,并且一个集合中的元素均大于另一个集合中的元素. 因此,我们考虑在一个任意的位置,将数组A划分成两部分.i表示划分数组A的位置,如果数组A包 ...

随机推荐

  1. locate,find,df,mount,du命令

    1.locate找数据的时候,相当于去这个数据库里面查(locate查找的时候不扫描磁盘)查找图标文件:locate .icolocat -i 不区分大小写创建一个文件,该文件没有在数据库中,要想在数 ...

  2. Inoic 滚动条问题

    1.看图说话 2.没有超过一个页,怎样去掉图中的滚动条? 3修改后预览效果

  3. MUI使用vue示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  4. MyBatis-Spring(五)--MapperScannerConfigurer实现增删改查

    上一篇文章中已经介绍,MapperScannerConfigurer可以通过扫描的方式获取我们需要的mapper,而不需要我们自己去配置,它的基本配置如下: <bean class=" ...

  5. curl下载安装与使用

    下载: https://curl.haxx.se/download.html 安装: 二进制安装.即解压即可. 使用 1.获取页面内容. 不加任何参数时,默认会发送GET请求来获取url内容到标准输出 ...

  6. 移动端H5适配流程

    (一) 由于手机生产商越来越多,不同手机的硬件尺寸又不尽相同,这就给我们的设计适配造成很大困扰.但我们可以围绕从基准分辨率设计,上下进行兼容适配的原则来进行快捷操作.以IOS阵营为例: 图注:移动适配 ...

  7. 20190922-雅礼Day2

    先送大家几个变量名: 具体的可以去$C++ \ Reference$里看(本页 右侧/下侧 有链接) 或者等一下奇迹银桥第三氮 const int c; mutable int a; volatile ...

  8. [LOJ2114][HNOI2015]-菜肴制作-拓扑排序+贪心

    <题面> 一个蒟蒻的痛苦一天 在今天的节目集训中,麦蒙将带领大家学习9种错误的解题策略 $15\%$算法(看两个就往下走吧) 1> puts("Impossible!&qu ...

  9. SpringBoot-(10)配置虚拟路径-指定外部路径文件夹存取文件

    参考:https://blog.csdn.net/feng2147685/article/details/95623135 package com.online.director; import or ...

  10. Python子进程 (subprocess包)

    Python子进程 (subprocess包) subprocess以及常用的封装函数 当我们运行python的时候,我们都是在创建并运行一个进程.正如我们在Linux进程基础中介绍的那样,一个进程可 ...