diff --git a/C++/004.Median of Two Sorted Arrays.cpp b/C++/004.Median of Two Sorted Arrays.cpp new file mode 100644 index 0000000..c1979e7 --- /dev/null +++ b/C++/004.Median of Two Sorted Arrays.cpp @@ -0,0 +1,52 @@ +4. Median of Two Sorted Arrays +There are two sorted arrays nums1 and nums2 of size m and n respectively. + +Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). + +You may assume nums1 and nums2 cannot be both empty. + +Example 1: + +nums1 = [1, 3] +nums2 = [2] + +The median is 2.0 +Example 2: + +nums1 = [1, 2] +nums2 = [3, 4] + +The median is (2 + 3)/2 = 2.5 + + +class Solution { +public: + double findMedianSortedArrays(vector& nums1, vector& nums2) { + int len1 = nums1.size(); + int len2 = nums2.size(); + int mid_len = (int)(len1+len2) /2+1; + //create an array to store the values of half of nums1 and nums2. + float store[mid_len] = {0}; + int k =0; + int i=0,j=0; + while(i 10) { @@ -65,4 +72,4 @@ class Solution { return (int)ans; } } -}; \ No newline at end of file +};