给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 。

请找出这两个有序数组的中位数。要求算法的时间复杂度为 O(log (m+n)) 。

示例 1:
nums1 = [1, 3]
nums2 = [2]
中位数是 2.0

示例 2:
nums1 = [1, 2]
nums2 = [3, 4]
中位数是 (2 + 3)/2 = 2.5

 class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
int allnums = nums1.size() + nums2.size();
int pos = allnums / ;
int count = ;
int p1 = , p2 = ;
vector<int> save; if (nums1.size() == && nums2.size() == ) return ; while (count <= pos)
{
if ((p1+)>nums1.size())
{
save.push_back(nums2[p2]);
p2++;
count++;
continue;
} if ((p2+)>nums2.size())
{
save.push_back(nums1[p1]);
p1++;
count++;
continue;
} if (nums1[p1] >= nums2[p2])
{
save.push_back(nums2[p2]);
p2++;
count++;
}
else
{
save.push_back(nums1[p1]);
p1++;
count++;
} } if (allnums % == ) return save[save.size() - ];
else return (save[save.size() - ] + save[save.size() - ]) / (2.0);
}
};

LeetCode刷题-004两个排序数组的中位数的更多相关文章

  1. C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4005 访问. 给定两个大小为 m 和 n 的有序数组 nums1 ...

  2. 从0打卡leetcode之day 5 ---两个排序数组的中位数

    前言 我靠,才坚持了四天,就差点不想坚持了.不行啊,我得把leetcode上的题给刷完,不然怕是不好进入bat的大门. 题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . ...

  3. Leetcode(4)-两个排序数组的中位数

      给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1: nums1 = [1, 3] ...

  4. C#LeetCode刷题之#26-删除排序数组中的重复项(Remove Duplicates from Sorted Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3622 访问. 给定一个排序数组,你需要在原地删除重复出现的元素, ...

  5. #leetcode刷题之路34-在排序数组中查找元素的第一个和最后一个位置

    给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置.你的算法时间复杂度必须是 O(log n) 级别.如果数组中不存在目标值,返回 [-1 ...

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

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

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

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

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

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

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

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

随机推荐

  1. IBM developer:Setting up the Kafka plugin for Ranger

    Follow these steps to enable and configure the Kafka plugin for Ranger. Before you begin The default ...

  2. 03 前端篇(JS)

    参考博客:http://www.cnblogs.com/yuanchenqi/articles/5980312.html JavaScript包括三部分: ECMAScript.DOM.BOM Jav ...

  3. openstack 2019/4/28

    官网参考地址:https://docs.openstack.org/keystone/queens/install/index-rdo.html (但愿能看懂) 环境:这个部分解释如何按示例架构配置控 ...

  4. Linux根据端口号查看进程PID

    1.命令lsof,以查找占用端口80为例,用法如下: [root@localhost nginx]# lsof -i:80 以上为没有进程占用80端口, [root@localhost sbin]# ...

  5. C语言报错:error: expected ‘while’ at end of input } ^

    在建线程池过程当中遇见上图所示错误: 解决方法: Linux中定义: SYNOPSIS #include <pthread.h> void pthread_cleanup_push(voi ...

  6. Jquery mobile中用Jquery的append()追加的内容没有Jquery mobile的样式

    Jquery Mobile 动态添加块之后, 样式不是JM内定的样式,解决方案如下: $('#content').append(html).enhanceWithin();//Jquery Mobil ...

  7. mysql数据库连接语句一定要加传参的编码格式

    jdbc:mysql://localhost:3306/pachong?useUnicode=true&characterEncoding=UTF-8 不然可能会出现从源代码传出的中文参数到m ...

  8. react 事件绑定 es5/es6

    // vscode shift + ctrl + v 预览 es 5 写法 无参函数的绑定 first methods 定义函数: handleClick(e) { // e - 事件对象 e.pre ...

  9. Nginx从入门到实践(四)

    Nginx常见问题和排错经验,实践应用场景中的方法处理Nginx安全,常见的应用层安全隐患,复杂访问控制,Nignx的sql防注入安全策略,Nginx的整体配置,搭建合理Nginx中间件架构配置步骤. ...

  10. [hosts]在hosts中屏蔽一级域名和二级域名的写法

    一级域名,如baidu: 0.0.0.0 baidu.com 二级域名 如有道公开课 0.0.0.0 ke.youdao.com 不带协议名,不带www. 用127.0.0.1也可以.