LeetCode刷题-004两个排序数组的中位数
给定两个大小为 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两个排序数组的中位数的更多相关文章
- C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4005 访问. 给定两个大小为 m 和 n 的有序数组 nums1 ...
- 从0打卡leetcode之day 5 ---两个排序数组的中位数
前言 我靠,才坚持了四天,就差点不想坚持了.不行啊,我得把leetcode上的题给刷完,不然怕是不好进入bat的大门. 题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . ...
- Leetcode(4)-两个排序数组的中位数
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 . 请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) . 示例 1: nums1 = [1, 3] ...
- C#LeetCode刷题之#26-删除排序数组中的重复项(Remove Duplicates from Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3622 访问. 给定一个排序数组,你需要在原地删除重复出现的元素, ...
- #leetcode刷题之路34-在排序数组中查找元素的第一个和最后一个位置
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置.你的算法时间复杂度必须是 O(log n) 级别.如果数组中不存在目标值,返回 [-1 ...
- LeetCode(4):两个排序数组的中位数
Hard! 题目描述: 有两个大小为 m 和 n 的排序数组 nums1 和 nums2 . 请找出两个排序数组的中位数并且总的运行时间复杂度为 O(log (m+n)) . 示例 1: nums1 ...
- LeetCode4. 两个排序数组的中位数
4. 两个排序数组的中位数 问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the ...
- 2.Median of Two Sorted Arrays (两个排序数组的中位数)
要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...
- LeetCode-4. 两个排序数组的中位数(详解)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 有两个大小为 m 和 n 的排序数组 nums ...
随机推荐
- 添加python虚拟环境
在我centos上装有两个python版本 # 我在~/py3/目录下创建虚拟环境,该目录为python3的一个独立环境 [root@localhost /]# cd home [root@local ...
- [LeetCode] 18. 四数之和
题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...
- 15 Django REST Framework 给api添加自定义搜索条件
一.ListModelMixin源码 # 源码 class ListModelMixin(object): """ List a queryset. "&quo ...
- AtCoder ABC 042D いろはちゃんとマス目 / Iroha and a Grid
题目链接:https://abc042.contest.atcoder.jp/tasks/arc058_b 题目大意: 给定一个 H * W 的矩阵,其中左下角 A * B 区域是禁区,要求在不踏入禁 ...
- Conda常见命令
Anaconda,Miniconda,Conda,Pip的区别: Anaconda:用于科学计算的python发行版,里面预装好了conda,某个版本的python,众多packages,科学计算工具 ...
- MongoDB ODBC Driver for Data Integration with Power BI
This guide will walk you through connecting Microsoft Power BI to a MongoDB DataSet using our MongoD ...
- Vjudge Code
Stylus @-moz-document url-prefix("https://cn.vjudge.net/"), url-prefix("https://vjudg ...
- java回调函数,看完就懂
java回调函数在网上了看了些例子,比较绕,不够清晰,自己写的一个例子比较通俗,java回调其实很简单. 举个例子我是类B,我有个方法叫b(),现在我要调用类A中的方法a(),写个代码就是: publ ...
- 制作OSGB数据索引
[干货]教你用.S3C文件制作OSGB数据索引 [干货]教你用.S3C文件制作OSGB数据索引_搜狐汽车_搜狐网 S3C是ContextCapture(原Smart 3D)的一种数据格式,.S3C格式 ...
- MT【324】增量代换
实数$a,b,c$满足$a^2+b^2+c^2=1$求$f=\min\{(a-b)^2,(b-c)^2,(c-a)^2\}$的最大值 分析:由对称性不妨设$c\ge b\ge a$,令$b-a=s,c ...