# 4. Median of Two Sorted Arrays

http://blog.csdn.net/beiyeqingteng/article/details/7533304/

class Solution {
private:
int findK(vector<int>& nums1,int pa,vector<int>& nums2,int setpa,int k)
{
int pb=k-1-pa;
int PA_1=(pa==0)?INT_MIN:nums1[pa-1];
int PB_1=(pb==0)?INT_MIN:nums2[pb-1];
int PA=(pa==nums1.size())?INT_MAX:nums1[pa];
int PB=(pb==nums2.size())?INT_MAX:nums2[pb];
if(PA>=PB_1&&PB>=PA) return PA;
if(PB>=PA_1&&PA>=PB) return PB;
if(PA>PB)
{
pa=((k-1-(pa-setpa))>nums2.size())?(k-1-nums2.size()):(pa-setpa);
return findK(nums1,pa,nums2,(setpa+1)/2,k);
}
else
{
pa=((pa+setpa)>nums1.size())?nums1.size():pa+setpa;
return findK(nums1,pa,nums2,(setpa+1)/2,k);
}
}
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
int l1=nums1.size();
int l2=nums2.size();
int l=l1+l2;
if(l%2==0)
{
int k=l/2;
int pa=min((int)nums1.size(),k-1);
int target1=findK(nums1,pa,nums2,(pa+1)/2,k);
k=l/2+1;
pa=min((int)nums1.size(),k-1);
int target2=findK(nums1,pa,nums2,(pa+1)/2,k);
return (target1+target2)/2.0;
}
else
{
int k=(l+1)/2;
int pa=min((int)nums1.size(),k-1);
return findK(nums1,pa,nums2,(pa+1)/2,k)*1.0;
}
}

};

#### leetcode之 median of two sorted arrays

2013-09-10 00:15:50

#### leetcode 4. Median of Two Sorted Arrays O(log(m+n))解法

2016-09-03 20:21:42

#### LeetCode（4）Median of Two Sorted Arrays

2015-08-22 20:35:01

#### LeetCode4—Median of Two Sorted Arrays

2017-09-05 14:50:39

#### 两个有序数组的中位数Median of Two Sorted Arrays（很重要）

2016-06-21 11:11:03

#### Leetcode解题报告：4. Median of Two Sorted Arrays

2016-09-20 01:48:28

#### leetcode之Median of Two Sorted Arrays问题

2017-06-29 18:49:50

#### leetcode 4 Median of Two Sorted Arrays c语言实现

2017-07-18 16:08:36

#### Median of Two Sorted Arrays 两个有序数组的中位数@LeetCode

2013-12-16 13:52:04

#### *Median of Two Sorted Arrays

2014-05-19 14:05:32