Last active
September 8, 2018 03:13
-
-
Save zhangxiaomu01/53e02717057f35c33aada91386ccf8ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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