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

==============

找出两个数组的中位数,

中位数的定义是明确的:按序排好的数组,array.size=n是奇数的话,第(n+1)/2个数字;否则就是第n/2和第(n+1)/2的平均数。

==========思路:

划分思路,

第一步:按照归并思路找到两个数组中的第k大

第二步:按照计算中位数的方法,返回中位数。

=========

code实现,

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int lengtha = nums1.size();
int lengthb = nums2.size();
int total = lengtha+lengthb;
if(total & 0x1){
return find_kth(nums1,nums2,total/+);
}else{
return (find_kth(nums1,nums2,total/)+
find_kth(nums1,nums2,total/+))/2.0;
}
}
private:
int find_kth(vector<int> &A,vector<int> &B,int k){
std::vector<int>::const_iterator p1 = A.begin();
std::vector<int>::const_iterator p2 = B.begin();
int m = ;
while(p1!=A.end() && p2!=B.end()){
if(*p1<=*p2 && m==(k-)){
return *p1;
}else if(*p1>*p2 && m==(k-)){
return *p2;
}
if(*p1<=*p2)
p1++;
else
p2++;
m++;
}//while
while(p1!=A.end()){
if(m==(k-)){
return *p1;
}
p1++;
m++;
}
while(p2!=B.end()){
if(m==(k-)){
return *p2;
}
p2++;
m++;
}
return -;
}
};

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. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  3. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  4. 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)

    转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...

  5. LeetCode 4 Median of Two Sorted Arrays (两个数组的mid值)

    题目来源:https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 an ...

  6. No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

  7. leetcode第四题:Median of Two Sorted Arrays (java)

    Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...

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

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

  9. Kotlin实现LeetCode算法题之Median of Two Sorted Arrays

    题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...

  10. LeetCode--No.004 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays Total Accepted: 104147 Total Submissions: 539044 Difficulty: Hard The ...

随机推荐

  1. href="#"与javascript:void(0)的区别

    共同点:都是一个空链接. 不同点:所以,#与javascript:void(0)的区别也很明显,#方法会跳转到页面的顶部,并且在页面URL后面会出现#,而javascript:void(0)方法不会, ...

  2. UVa 二叉树的编号(二叉树)

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. 机器翻译 2010年NOIP全国联赛提高组

    题目描述 Description 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义 来替换.对于每个英 ...

  4. 输入DStream和Receiver详解

    输入DStream代表了来自数据源的输入数据流.在之前的wordcount例子中,lines就是一个输入DStream(JavaReceiverInputDStream),代表了从netcat(nc) ...

  5. CentOS 中PHP开启 GD功能

    yum install php-gd 然后重启服务器: service httpd restart

  6. meta name="viewport" 属性详解

    随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移动互联应用开发也越来越受到人们的重视,用html5开发移动应用是最好的选择.然而,每一款手机有不同的分辨率,不同屏幕 ...

  7. 26 个 jQuery使用技巧

    1. 禁用右键点击(Disable right-click) $(document).ready(function(){ $(document).bind("contextmenu" ...

  8. 七牛云存储Python SDK使用教程 - 上传策略详解

    文 七牛云存储Python SDK使用教程 - 上传策略详解 七牛云存储 python-sdk 七牛云存储教程 jemygraw 2015年01月04日发布 推荐 1 推荐 收藏 2 收藏,2.7k  ...

  9. C++文件输入和输出

    1.引入头文件fstreamfstream头文件定义了用于文件输入的类ifstream和文件输出的类ofstream 2.写文件1)创建一个ofstream对象来管理输出流2)将该对象与文件关联起来3 ...

  10. IIS用户权限备忘

    经常在网站部署到IIS遇到IIS帐户没有权限的问题,总是在看IIS Admin Service,但发现些帐户是有权限的. 其实针对相应的站点,应该看的是Application Pool的运行帐户,这个 ...