https://leetcode.com/problems/median-of-two-sorted-arrays/

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)).

Example 1:

nums1 = [1, 3]
nums2 = [2] The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 第一种思路:采用最暴力的方法,因为给的两个数组都已经排序了,也知道中位数是第几个,直接采用二分归并排序到中位数。
 class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
vector<int> nums3;
int i=,j=,k=;
int m =(nums1.size()+nums2.size());
while(i<nums1.size()&&j<nums2.size()) {
if(nums1[i]<nums2[j])
nums3.push_back(nums1[i++]);
else
nums3.push_back(nums2[j++]);
k++;
if(k>m/)
{
if(m%)
return nums3[k-];
else
return (nums3[k-]+nums3[k-])/2.0;
}
}
while(i<nums1.size()){
nums3.push_back(nums1[i++]);
k++;
if(k>m/)
{
if(m%)
return nums3[k-];
else
return (nums3[k-]+nums3[k-])/2.0;
}
}
while(j<nums2.size()){
nums3.push_back(nums2[j++]);
k++;
if(k>m/)
{
if(m%)
return nums3[k-];
else
return (nums3[k-]+nums3[k-])/2.0;
}
}
return ;
}
};

在leetcode中时间为48ms。

												

leetcode 4. Median of Two Sorted Arrays的更多相关文章

  1. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

  2. LeetCode(3) || Median of Two Sorted Arrays

    LeetCode(3) || Median of Two Sorted Arrays 题记 之前做了3题,感觉难度一般,没想到突然来了这道比较难的,星期六花了一天的时间才做完,可见以前基础太差了. 题 ...

  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 ...

  4. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  5. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...

  6. 第三周 Leetcode 4. Median of Two Sorted Arrays (HARD)

    4. Median of Two Sorted Arrays 给定两个有序的整数序列.求中位数,要求复杂度为对数级别. 通常的思路,我们二分搜索中位数,对某个序列里的某个数 我们可以在对数时间内通过二 ...

  7. 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 ...

  8. 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 ...

  9. leetcode之 median of two sorted arrays

    这是我做的第二个leetcode题目,一开始以为和第一个一样很简单,但是做的过程中才发现这个题目非常难,给人一种“刚上战场就踩上地雷挂掉了”的感觉.后来搜了一下leetcode的难度分布表(leetc ...

随机推荐

  1. vs2012 发布网站时,发布目录为空

    当我使用Release Any CPU时为空 使用Release X86就正常发布了 奇怪. 之后再切换回 Release Any CPU时正常发布. 在生成时可以尝试设置好生成配置,先生成,再发布.

  2. html中空格转义字符

    记录一下,空格的转义字符分为如下几种: 平时一般用的是  1.  &160#;不断行的空白(1个字符宽度) 2.  &8194#;半个空白(1个字符宽度) 3.  &8195# ...

  3. ctype.h库函数----字符操作函数

    在c++中使用时:  #include <cctype> 字符判断函数 1.isalnum函数--判断是否是英文字母或数字字符,如果是,则返回非0值,如果不是,则返回0. 函数参数 :可以 ...

  4. php extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容

    extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容 它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具

  5. Javascript iframe交互并兼容各种浏览器的解决方案

    在Web前端开发中,我们经常会用到iframe这个控件. 但是这个控在内.外交互时,往往各个浏览器所用的关键字不同,很是麻烦,为了能够得到子iframe中的window对象,各家浏览器有着各家的指定, ...

  6. 在IIS7中ASP.NET MVC3网站中建立gcc编译服务时要注意的问题

    1. 正常配置下调用gcc总是失败,虽然在环境变量中配置了gcc路径,但提示找不到gcc编译程序: 2. 使用全路径调用gcc,也是失败,提示cc1.exe编译失败,失败代码53: 3. 通过远程桌面 ...

  7. 使用JQuery实现手风琴布局

    当鼠标在元素上悬停或移出时,可以通过JQuery的hover方法来给元素绑定两个处理方法,前者用于鼠标悬停,后者用于鼠标移出,因此根据这这特点再设置相应元素的css属性后就可以方便的实现手风琴布局,然 ...

  8. swiper

    <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...

  9. MS - 2 - 设计包含 min 函数的栈

    定义栈的数据结构,要求添加一个 min 函数,能够得到栈的最小元素. 要求函数 min.push 以及 pop 的时间复杂度都是 O(1). template<typename T> st ...

  10. a different object with the same identifier value was already associated with the session:错误;

    当出现a different object with the same identifier value was already associated with thesession时,一般是因为在h ...