1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int M = nums1.size(), N = nums2.size(), target_len = ceil((double)(M + N + 1) / 2), nums1_iter = 0, nums2_iter = 0; double left = -1, right = -1; for(int i = 0; i < target_len; ++i) { left = right; if(nums2_iter >= N || (nums1_iter < M && nums1[nums1_iter] <= nums2[nums2_iter])){ right = nums1[nums1_iter++]; } else { right = nums2[nums2_iter++]; } } if((M + N) % 2) return right; return (left + right) / 2; } };
|
Accepted
2085/2085 cases passed (72 ms)
Your runtime beats 6.51 % of cpp submissions
Your memory usage beats 5.16 % of cpp submissions (90.9 MB)