Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int pre = -1, current = -1;
int N1 = nums1.size(), N2 = nums2.size();
int aStart = 0, bStart = 0;
int total = N1 + N2;
for(int i=0; i<total/2 + 1; i++)
{
pre = current;
if(aStart < N1 && (bStart >= N2||nums1[aStart] < nums2[bStart]))
{
current = nums1[aStart++];
}
else
current = nums2[bStart++];
}
if((total&1) == 0)
return (pre+current)/2.0;
else
return current;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment