给定两个大小为 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. ​Installing the Ranger Kafka Plug-in

    This section describes how to install and enable the Ranger Kafka plug-in. The Ranger Kafka plug-in ...

  2. 网络流 P2770 航空路线问题

    #include <cstdio> #include <cstdlib> #include <map> #include <queue> #includ ...

  3. 六 Struts 拦截器、OGNL表达式

    一.OGNL表达式1.概念:是表达式语言,专门用来访问对象取值用的.2.对比EL表达式使用场景: A.EL主要用在web的jsp页面取值 B.OGNL适用以下环境 1.java程序中 2.在页面使用( ...

  4. IDEA导入项目jar包红线、依赖问题....

    一般遇到这种情况用以下两种方式解决....... 1.reimport包 2.清缓存重启 针对1方法: a.确实不缺包: 可以先注释掉pom文件中的jar包, 此时idea会提示import, 那就i ...

  5. Counting Sort(Java)

    public static void countingsort(int[] a, int n) //n = a.length { int max = a[0], min = a[0]; for(int ...

  6. Linux操作系统计划任务

    ++++++++++++++++++++++++++++++++++++++++++++++++标题:Linux操作系统的计划任务内容:计划任务分为单次任务和周期性任务,周期任务分为系统级计划任务和用 ...

  7. 关联分析中寻找频繁项集的FP-growth方法

    关联分析是数据挖掘中常用的分析方法.一个常见的需求比如说寻找出经常一起出现的项目集合. 引入一个定义,项集的支持度(support),是指所有包含这个项集的集合在所有数据集中出现的比例. 规定一个最小 ...

  8. win10双系统安装卸载ubuntu

    安装 1. 官网下载需要安装的Ubuntu版本 2. 格式化U盘,用UltraISO软件将Ubuntu写入U盘 3. 设置电脑U盘启动,重启电脑安装,注意安装时关闭在线下载,否则会安装很久 4. 安装 ...

  9. Adding appsettings.json to a .NET Core console app

    This is something that strangely doesn’t seem to be that well documented and took me a while to figu ...

  10. 2018 API变化