Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Last active September 8, 2018 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/53e02717057f35c33aada91386ccf8ed to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/53e02717057f35c33aada91386ccf8ed to your computer and use it in GitHub Desktop.
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