My idea is that, since it is an ordered array, we can follow the idea of merging the sorting method, according to the final merging process.
Creates a new array, numbering the current positions of the two arrays and the merged array i,j,k.
Push the smaller values of the two arrays into the position of I, then the array of the extracted data and the merged array index +1.
The question to consider in this process is what to do if one of the arrays has been fully fetched.
Here's My Code:
classSolution { Public: DoubleFindmediansortedarrays (vector<int>& Nums1, vector<int>&nums2) {size_t n= Nums1.size (), m=nums2.size (); Vector<int>Combine; intI=0, j=0, k=0; for(; i<m+n;++i) { if(j<n&&k<m) combine.push_back (Nums1[j]<nums2[k]?nums1[j++]:nums2[k++]); ElseCombine.push_back (J==n?nums2[k++]:nums1[j++]); } if((m+n)%2==1) returncombine[(m+n-1)/2]; Else return(Combine[(m+n-1)/2]+combine[(m+n-1)/2+1])/2.0; }};
Leetcode 4: Given two ordered arrays, find the median value of two arrays.