In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to find the median of the two sorted arrays, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.
I. explanation
Given two ordered arrays nums1 and nums2 of sizes m and n.
Please find the median of these two ordered arrays. The time complexity of the algorithm is required to be O (log (massin)).
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
II. Solution reference
1. Swift language
/ / method 1: class Solution {func findMedianSortedArrays (nums1: [Int], _ nums2: [Int])-> Double {let m = nums1.count let n = nums2.count if m > n {return findMedianSortedArrays (nums2, nums1)} var halfLength: Int = (m + n + 1) > > 1 var b = 0 E = m var maxOfLeft = 0 var minOfRight = 0 while b > 1 let mid2 = halfLength-mid1 if mid1 > 0 & & mid2
< n && nums1[mid1 - 1] >Nums2 [mid2] {e = mid1-1} else if mid2 > 0 & & mid1
< m && nums1[mid1] < nums2[mid2 - 1] { b = mid1 + 1 } else { if mid1 == 0 { maxOfLeft = nums2[mid2 - 1] } else if mid2 == 0 { maxOfLeft = nums1[mid1 - 1] } else { maxOfLeft = max(nums1[mid1 - 1], nums2[mid2 - 1]) } if (m + n) % 2 == 1 { return Double(maxOfLeft) } if mid1 == m { minOfRight = nums2[mid2] } else if mid2 == n { minOfRight = nums1[mid1] } else { minOfRight = min(nums1[mid1], nums2[mid2]) } break } } return Double(maxOfLeft + minOfRight) / 2.0 }}// 方式二class MedianTwoSortedArrays { func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) ->Double {let m = nums1.count let n = nums2.count return (findKth (nums1, nums2, (m + n + 1) / 2) + findKth (nums1, nums2, (m + n + 2) / 2) / 2} private func findKth (_ nums1: [Int], _ nums2: [Int] _ index: Int)-> Double {let m = nums1.count let n = nums2.count guard m 0 and A [I-1] > B [j]: # i is too big Must decrease it imax = iMur1 else: # i is perfect if i = 0: max_of_left = B [j-1] elif j = 0: max_of_left = A [I-1] else: max_of_left = max (A [I-1] B [j-1]) if (m + n)% 2 = = 1: return max_of_left if I = = m: min_of_right = B [j] elif j = n: min_of_right = A [I] else: min_of_right = min (A [I], B [j]) return (max_of_left + min_of_right) / 2.0
3. Java language
Class Solution {public double findMedianSortedArrays (int [] A, int [] B) {int m = A. stories; int n = B. stories; if (m > n) {/ / to ensure m 伊明 & & A [I-1] > B [j]) {iMax = iMax-1; / / i is too big} else {/ / i is perfect int maxLeft = 0 If (I = = 0) {maxLeft = B [j-1];} else if (j = = 0) {maxLeft = A [I-1];} else {maxLeft = Math.max (A [I-1], B [j-1]);} if ((m + n)% 2 = = 1) {return maxLeft;} int minRight = 0 If (I = = m) {minRight = B [j];} else if (j = = n) {minRight = A [I];} else {minRight = Math.min (B [j], A [I]);} return (maxLeft + minRight) / 2.0;}} return 0.0;}}
4. C++ language
# include / / Classical binary search algorithm, but slightly different// if cannot find the key, return the position where can insert the key int binarySearch (int A [], int low, int high, int key) {while (low A [mid]) {low = mid + 1;} else {high = mid-1;}} return low / tes:// I feel the following methods is quite complicated, it should have a better high clear and readable solutiondouble findMedianSortedArrayHelper (int A [], int m, int B [], int n, int lowA, int highA, int lowB, int highB) {/ / Take the A [middle], search its position in B array int mid = lowA + (highA-lowA) / 2; int pos = binarySearch (B, lowB, highB, A [mid]); int num = mid + pos / / If the A [middle] in B is B's middle place, then we can have the result if (num = = (massin) / 2) {/ / If two arrays total length is odd, just simply return the A [mid] / / Why not return the B [pos] instead? / / suppose A = {1suppose 5} B = {2jue 4}, then mid=1, pos=1 / suppose A = {3je 5} B = {1men2cet 4}, then mid=0 Pos=2 / / suppose A = {1pje 3je 4je 5} B = {2}, then mid=1, pos=1 / / You can see, the `pos` is the place A [mid] can be inserted, so return A [mid] if ((mynn)% 2pm 1) {return A [mid] } / / If tow arrys total length is even, then we have to find the next one. Int next; / / If both `mid` and `pos` are not the first postion. / / Then, find max (A [mid-1], B [pos-1]). / / Because the `mid` is the second middle number, we need to find the first middle number / / Be careful about the edge case if (mid > 0 & & pos > 0) {next = A [mid-1] > B [pos-1]? A [mid-1]: B [pos-1];} else if (pos > 0) {next = B [pos-1];} else if (mid > 0) {next = A [mid-1];} return (A [mid] + next) / 2.0 } / / if A [mid] is in the left middle place of the whole two arrays / A (len=16) B (len=10) / / [.] [.] / / ^ ^ / / mid=7 pos=1 / move the `low` pointer to the "middle" position Do next iteration. If (num
< (m+n)/2){ lowA = mid + 1; lowB = pos; if ( highA - lowA >HighB-lowB) {return findMedianSortedArrayHelper (A, m, B, n, lowA, highA, lowB, highB);} return findMedianSortedArrayHelper (B, n, A, m, lowB, highB, lowA, highA);} / / if A [mid] is in the right middle place of the whole two arrays if (num > (MSN) / 2) {highA = mid-1; highB = pos-1 If (highA-lowA > highB-lowB) {return findMedianSortedArrayHelper (A, m, B, n, lowA, highA, lowB, highB);} return findMedianSortedArrayHelper (B, n, A, m, lowB, highB, lowA, highA);} double findMedianSortedArrays (int A [], int m, int B [], int n) {/ / checking the edge cases if / / if the length of array is odd, return the middle one / / if the length of array is even, return the average of the middle two numbers if (masks / 0) return n% 2 / 1? B [nqpr 2]: (B [nxpx 2-1] + B [nxpx 2]) / 2.0; if (nexus 2) return m% 2cm 1? A [and the shoter array be B if 2]: (a [m racer 2-1] + A [m racer 2]) / 2.0; / / let the longer array be A, and the shoter array be B if (m > n) {return findMedianSortedArrayHelper (A, m, B, n, 0, m Muth1, 0, NMAE 1);} return findMedianSortedArrayHelper (B, n, A, m, 0, n r r 1,0, m m 1) } int main () {int R1 [] = {1}; int R2 [] = {2}; int N1 = sizeof (R1) / sizeof (R1 [0]); int N2 = sizeof (R2) / sizeof (R2 [0]); printf ("Median is 1.5 =% f", findMedianSortedArrays (R1, N1, R2, N2)); int ar1 [] = {1,12,15,26,38}; int ar2 [] = {2,13,17,30,45,50} N1 = sizeof (ar1) / sizeof (ar1 [0]); N2 = sizeof (ar2) / sizeof (ar2 [0]); printf ("Median is 17 =% f", findMedianSortedArrays (ar1, N1, ar2, N2)); int ar11 [] = {1,12,15,26,38}; int ar21 [] = {2,13,17,30,45}; N1 = sizeof (ar11) / sizeof (ar11 [0]); N2 = sizeof (ar21) / sizeof (ar21 [0]) Printf ("Median is 16 =% f", findMedianSortedArrays (ar11, N1, ar21, N2); int A1 [] = {1,2,5,6,8}; int a2 [] = {13,17,30,45,50}; N1 = sizeof (A1) / sizeof (A1 [0]); N2 = sizeof (a2) / sizeof (a2 [0]); printf ("Median is 10.5 =% f", findMedianSortedArrays (A1, N1, a2, N2)) Int A10 [] = {1, 2, 5, 6, 8, 9, 10}; int A20 [] = {13, 17, 30, 45, 50}; N1 = sizeof (A10) / sizeof (A10 [0]); N2 = sizeof (A20) / sizeof (A20 [0]); printf ("Median is 9.5 =% f", findMedianSortedArrays (A10, N1, A20, N2); int A11 [] = {1,2,5,6,8,9} Int A21 [] = {13,17,30,45,50}; N1 = sizeof (A11) / sizeof (A11 [0]); N2 = sizeof (A21) / sizeof (A21 [0]); printf ("Median is 9 =% f", findMedianSortedArrays (A11, N1, A21, N2)); int A12 [] = {1,2,5,6,8}; int A22 [] = {11,13,17,30,45,50} N1 = sizeof (A12) / sizeof (A12 [0]); N2 = sizeof (A22) / sizeof (A22 [0]); printf ("Median is 11 =% f", findMedianSortedArrays (a12, N1, A22, N2)); int b1 [] = {1}; int b2 [] = {2 Magazine 4}; N1 = sizeof (b1) / sizeof (b1 [0]); N2 = sizeof (b2) / sizeof (b2 [0]) Printf ("Median is 2.5 =% f", findMedianSortedArrays (b1, N1, b2, N2); return 0;}
5. C language
# include # include static double find_kth (int a [], int alen, int b [], int blen, int k) {/ * Always assume that alen is equal or smaller than blen * / if (alen > blen) {return find_kth (b, blen, a, alen, k);} if (alen = = 0) {return b [k-1];} if (k = = 1) {return a [0]
< b[0] ? a[0] : b[0]; } /* Divide k into two parts */ int ia = k / 2 < alen ? k / 2 : alen; int ib = k - ia; if (a[ia - 1] < b[ib - 1]) { /* a[ia - 1] must be ahead of k-th */ return find_kth(a + ia, alen - ia, b, blen, k - ia); } else if (a[ia - 1] >B [ib-1]) {/ * b [ib-1] must be ahead of k-th * / return find_kth (a, alen, b + ib, blen-ib, k-ib);} else {return a [ia-1];}} static double findMedianSortedArrays (int* nums1, int nums1Size, int* nums2, int nums2Size) {int half = (nums1Size + nums2Size) / 2 If ((nums1Size + nums2Size) & 0x1) {return find_kth (nums1, nums1Size, nums2, nums2Size, half + 1);} else {return (find_kth (nums1, nums1Size, nums2, nums2Size, half) + find_kth (nums1, nums1Size, nums2, nums2Size, half + 1)) / 2;} int main (int argc, char * * argv) {int R1 [] = {1}; int R2 [] = {2} Int N1 = sizeof (R1) / sizeof (R1 [0]); int N2 = sizeof (R2) / sizeof (R2 [0]); printf ("Median is 1.5 =% f", findMedianSortedArrays (R1, N1, R2, N2); int ar1 [] = {1,12,15,26,38}; int ar2 [] = {2,13,17,30,45,50}; N1 = sizeof (ar1) / sizeof (ar1 [0]) N2 = sizeof (ar2) / sizeof (ar2 [0]); printf ("Median is 17 =% f", findMedianSortedArrays (ar1, N1, ar2, N2)); int ar11 [] = {1,12,15,26,38}; int ar21 [] = {2,13,17,30,45}; N1 = sizeof (ar11) / sizeof (ar11 [0]); N2 = sizeof (ar21) / sizeof (ar21 [0]) Printf ("Median is 16 =% f", findMedianSortedArrays (ar11, N1, ar21, N2); int A1 [] = {1,2,5,6,8}; int a2 [] = {13,17,30,45,50}; N1 = sizeof (A1) / sizeof (A1 [0]); N2 = sizeof (a2) / sizeof (a2 [0]); printf ("Median is 10.5 =% f", findMedianSortedArrays (A1, N1, a2, N2)) Int A10 [] = {1, 2, 5, 6, 8, 9, 10}; int A20 [] = {13, 17, 30, 45, 50}; N1 = sizeof (A10) / sizeof (A10 [0]); N2 = sizeof (A20) / sizeof (A20 [0]); printf ("Median is 9.5 =% f", findMedianSortedArrays (A10, N1, A20, N2); int A11 [] = {1,2,5,6,8,9} Int A21 [] = {13,17,30,45,50}; N1 = sizeof (A11) / sizeof (A11 [0]); N2 = sizeof (A21) / sizeof (A21 [0]); printf ("Median is 9 =% f", findMedianSortedArrays (A11, N1, A21, N2)); int A12 [] = {1,2,5,6,8}; int A22 [] = {11,13,17,30,45,50}; return 0 } after reading the above, do you have any further understanding of how to find the median of the two sorted arrays? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.