给定两个大小为 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. 【核心核心】8.Spring【AOP】注解方式

    1.引入jar包 sprig框架基础包+JUntil整合包+日志包+AOP包 spring的传统AOP的开发的包 spring-aop-4.2.4.RELEASE.jar com.springsour ...

  2. router-link动态赋值

    A:router路由配置 export default new Router({ routes: [ { path: '/home', name: 'Home', component: Home, c ...

  3. vue 获取当前元素

    获取当前元素 Html: <li><a href="#" v-on:click="typeStyle">萨克斯萨克<span> ...

  4. jeecms 链接标签

    .引入页面 [#include "../include/header-site.html"/]12.导航栏只有前两个带链接 [#if c_index<2] href=&quo ...

  5. NGINX模块开发 之 验证URL參数

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/RoyalApex/article/details/26404379 作者:邹祁峰 邮箱:Qifeng ...

  6. Object上的静态方法

    内置提供了一个对象为 Object ,也被称之为是构造函数,用来创建对象用的.在 javascript 函数也是对象,是一种可被执行的对象,所以称Object为对象也是可以的.挂在函数上的方法,称之为 ...

  7. Redis学习03——存储字符串(String)

    --------------------- 作者:愤怒的小明 来源:CSDN 原文:https://blog.csdn.net/qiwenmingshiwo/article/details/78118 ...

  8. pytorch 多GPU处理过程

    多GPU的处理机制: 使用多GPU时,pytorch的处理逻辑是: 1.在各个GPU上初始化模型. 2.前向传播时,把batch分配到各个GPU上进行计算. 3.得到的输出在主GPU上进行汇总,计算l ...

  9. Luogu P2831 愤怒的小鸟(状压+记忆化搜索)

    P2831 愤怒的小鸟 题意 题目描述 Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于\((0,0)\)处,每次Kiana可以用它向第一象限发射 ...

  10. 时序数据库连载系列: 时序数据库一哥InfluxDB之存储机制解析

    InfluxDB 的存储机制解析 本文介绍了InfluxDB对于时序数据的存储/索引的设计.由于InfluxDB的集群版已在0.12版就不再开源,因此如无特殊说明,本文的介绍对象都是指 InfluxD ...