class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n1=nums1.size(),n2=nums2.size();
int mid=(n1+n2)>>1,i=0,j=0;
mid+=(n1+n2)%2==1?1:0;
int t=0,flag;
while(t<mid){
if(i<n1&&j<n2){
if(nums1[i]<nums2[j]){i++;flag=0;}
else {j++;flag=1;}
}
else {
if(j<n2){j++;flag=1;}
else {i++;flag=0;}
}
t++;
}
int premid= flag==1?nums2[j-1]:nums1[i-1];
if((n1+n2)%2){
return premid;
}
else{
int behmid;
if(i>=n1) behmid=nums2[j];
else if(j>=n2) behmid=nums1[i];
else behmid=nums1[i]<nums2[j]?nums1[i]:nums2[j];
return float(behmid+premid)/2;
}
return nums1[i-1];
}
};