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. Prepared Java infrastructure for distributed scenarios

    code is sited on: https://github.com/zhoujiagen/javaiospike progress 2015/5/27 Nio/Nio2 examples, us ...

  2. 274. H-Index

    Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...

  3. Codeforces Round #377 (Div. 2) A B C D 水/贪心/贪心/二分

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  4. 资源预加载 Preload

    当提到前端性能优化时,我们首先会联想到文件的合并.压缩,文件缓存和开启服务器端的 gzip 压缩等,这使得页面加载更快,用户可以尽快使用我们的 Web 应用来达到他们的目标. 资源预加载 是另一个性能 ...

  5. UpdatePanel与$.function()同时使用问题

    在.NET中使用了UpdatePanel,里面的输入框使用了jQuery的日历选择器,接下来介绍下两者同时使用的一些细节及问题的解决方法,感兴趣的各位可以参考下哈 今天,在.NET中使用了Update ...

  6. Ci分开配置网站前台后台的方法

    CodeIgniter 是一个简单快速的PHP MVC框架.EllisLab 的工作人员发布了 CodeIgniter.许多企业尝试体验过所有 PHP MVC 框架之后,CodeIgniter 都成为 ...

  7. Linux-SSL和SSH和OpenSSH,OpenSSL有什么区别

    ssl是通讯链路的附加层.可以包含很多协议.https, ftps, ..... ssh只是加密的shell,最初是用来替代telnet的.通过port forward,也可以让其他协议通过ssh的隧 ...

  8. MySQL 常用show命令

    MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中对show命令的使用还容易产生混淆,本文汇集了show命令的众多用法. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  9. JS的Document属性和方法小结

    Document想必大家并不陌生吧,在使用js的过程中会经常遇到它,那么它有哪些属性.哪些方法,在本文将以示例为大家详细介绍下,希望对大家有所帮助 document.title //设置文档标题等价于 ...

  10. 解析C#中[],List,Array,ArrayList的区别及应用

    [] 是针对特定类型.固定长度的. List 是针对特定类型.任意长度的. Array 是针对任意类型.固定长度的. ArrayList 是针对任意类型.任意长度的. Array 和 ArrayLis ...