【一天一道LeetCode】#4 Median of Two Sorted Arrays
一天一道LeetCode
(一)题目
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
给定两个排好序的数组,求两个数组的中间值。
例如:[1 2]和[1 2] 返回值:1.5
(二)解题思路
时间复杂度要满足O(log(m+n)), 可以采用一个辅助容器来存储小值,等存到两个数组的一半的时候就停止,再根据奇偶来求中间值。
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
vector<int> temp;
int len1=nums1.size();
int len2= nums2.size();
int idx = (len1+len2)/2+1;//需要存放idx个数
int i =0,j=0;
int size = 0;
if(len1 ==0 && len2==0) return 0.0;
while(size != idx) //当idx==size的时候退出
{
if(i == len1 && j<len2)//1遍历完了还没有找到
{
temp.push_back(nums2[j]);
j++;
}
else if(i < len1 && j==len2)//2遍历完了还没有找到
{
temp.push_back(nums1[i]);
i++;
}
else if(i < len1 && j<len2)//1和2都没有遍历完
{
if(nums1[i]<=nums2[j]){
temp.push_back(nums1[i]);
i++;
}
else
{
temp.push_back(nums2[j]);
j++;
}
}
size = temp.size();
}
if((len1+len2)%2 == 1) return (double)temp[idx-1];
else return (double)(temp[idx-1]+temp[idx-2])/2;
}
};
该算法遍历(m+n)/2+1次,所以时间复杂度为O(m+n)。
提示:Accepted!
和女票一起做的这题,一开始我用两个指针来求中间值,无奈情况太多考虑不周,代码量太大了,后来才转而用辅助vector。女票半个小时不到就做出来了,用STL的multiset几句代码就搞定的,multiset的insert自带排序,简直逆天!下面贴上她的代码。
#include<set>
class Solution {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int mid = (m+n+1)/2-1, median1 = 0, median2 = 0;
multiset<int> mergeArray;
for(int i = 0; i < m; i++)
mergeArray.insert(A[i]);
for(int i = 0; i < n; i++)
mergeArray.insert(B[i]);
int i = 0;
set<int>::iterator iter = mergeArray.begin();
for(; i++ < mid &&iter != mergeArray.end(); ++iter);
median1 = *iter;
iter++;
median2 = *iter;
if((m+n)%2)
return median1;
else
return (median1+median2)/2.0;
}
};
【一天一道LeetCode】#4 Median of Two Sorted Arrays的更多相关文章
- 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...
- LeetCode(3) || Median of Two Sorted Arrays
LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...
- LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)
题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- LeetCode 4. Median of Two Sorted Arrays & 归并排序
Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...
- 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)
4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...
- Leetcode 4. Median of Two Sorted Arrays(中位数+二分答案+递归)
4. Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n resp ...
- LeetCode 004 Median of Two Sorted Arrays
题目描述:Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. F ...
- leetcode 4. Median of Two Sorted Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...
- leetcode之 median of two sorted arrays
这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...
随机推荐
- The new powerful SQL executing schedule monthly or weekly in DB Query Analyzer 7.01
1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainland. ...
- Linux 高性能服务器编程——Linux网络编程基础API
问题聚焦: 这节介绍的不仅是网络编程的几个API 更重要的是,探讨了Linux网络编程基础API与内核中TCP/IP协议族之间的关系. 这节主要介绍三个方面的内容:套接字(so ...
- ubuntu挂载的NTFS文件编译失败问题
错误: 编译Android源代码时候出现,权限拒绝的错误 解决方法: sudo apt-get install ntfs-config sudo ntfs-config 我的微信二维码如下,欢迎交流讨 ...
- css模块化及CSS Modules使用详解
什么是css模块化? 为了理解css模块化思想,我们首先了解下,什么是模块化,在百度百科上的解释是,在系统的结构中,模块是可组合.分解和更换的单元.模块化是一种处理复杂系统分解成为更好的可管理模块的方 ...
- FFmpeg源代码简单分析:avcodec_encode_video()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- 【移动开发】plurals
不同的语言对数量的语法规定有不同的规则.在英语里面,例如,1是特例.我们会直接写1book,而针对一个以上的我们会在book后加复数形式.这种区别对单数和复数来说是很普遍的,但是其他的语言做了更好的区 ...
- iOS下JS与OC互相调用(一)--UIWebView 拦截URL
最近准备把之前用UIWebView实现的JS与原生相互调用功能,用WKWebView来替换.顺便搜索整理了一下JS 与OC 交互的方式,非常之多啊.目前我已知的JS 与 OC 交互的处理方式: * 1 ...
- SSH深度历险(五) 深入浅出-----IOC AND AOP
IOC就是Inversion of Control,控制反转.在Java开发中,IoC意味着将你设计好的类交给系统(容器)来控制实现,而不是在你的类内部控制.这称为控制反转. 本人理解:就是把原本你自 ...
- INV 调试: 如何获取库存物料事务处理调试信息
1. 按如下方式设置系统配置文件值: 系统配置文件值 地点/用户/应用/职责层配置文件值 --汇总 FND: 启用调试日志 是 FND:调试日志层级 陈述 INV: 调试跟踪: 是 IN ...
- Android对话框AlertDialog-android学习之旅(四十二)
对话框简介 android提供了丰富的对话框支持,支持四种如下的对话框. AlertDialog简介 介绍上面六个方法的代码示例 setMessage() <?xml version=" ...