给定两个大小为 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. 数据库【redis】基本命令

    redis常用命令大全   1.基于内存的key-value数据库 2.基于c语言编写的,可以支持多种语言的api //set每秒11万次,取get 81000次 3.支持数据持久化 4.value可 ...

  2. button样式篇一(ant Design React)

    这篇来介绍button中elementUi.iview.ant中样式结构 ant Design react ant-react中button分两个文件less: mixins.less:根据butto ...

  3. centos6.8 安装jenkins

    1.使用yum安装java环境 #查看CentOS自带JDK是否已安装yum list installed |grep java #查看yum库中的Java安装包yum -y list java*#以 ...

  4. LVS调度算法

    LVS-四层调度 1.轮询算法:Round Robin - RR 后端RS性能一致,请求开销差别小 2.加权轮询:Weighted Round Robin - WRR 后端RS性能有差异,请求开销差异 ...

  5. 证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP)

    离散对数问题,英文是Discrete logarithm Problem,有时候简写为Discrete log,该问题是十几个开放数学问题(Open Problems in Mathematics, ...

  6. Mysql中的explain和desc

    查询分析器 desc 和 explain 作用基本一样,explain速度快一点 explain 一条SQL语句出出现以下参数, 其中id,select_type,table 用于定位查询,表示本行参 ...

  7. VS2013下载与安装

    1. 官网下载地址:  https://my.visualstudio.com/Downloads?q=visual studio 2013&wt.mc_id=o~msft~vscom~old ...

  8. button样式的demo

    <style type="text/css"> .styletop{margin-top: 200px;} .stylea{ margin-left:550px;} ; ...

  9. React笔记:组件(3)

    1. 组件定义 组件是React的核心概念,组件将应用的UI拆分成独立的.可复用的模块. 定义组件的两种方式: (1)类组件:使用ES6 class (2)函数组件:使用函数 使用class定义组件的 ...

  10. 2.nginx_rewrite模块

    rewrite syntax: rewrite regex replacement [flag] Default: — Context: server, location, if 如果正则表达式(re ...