In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Java how to find the median of two ordered arrays". In daily operation, I believe that many people have doubts about how Java finds the median of two ordered arrays. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to find the median of two ordered arrays". Next, please follow the editor to study!
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 (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
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)).
1. My idea is that there are a total of m + n. We can create a new List and put the minimum number in each. The code is as follows:
Class Solution: def findMedianSortedArrays (self, nums1 Nums2): ": type nums1: List [int]: type nums2: List [int]: rtype: float" leng = len (nums1) + len (nums2) tmpLen = leng//2 + 1 newList = [0] * tmpLen I = 0 j = 0 for k in range (tmpLen): if I = = len ( Nums1): newList [k] = nums2 [j] j + = 1 elif j = = len (nums2): newList [k] = nums1 [I] I + = 1 else: if nums1 [I]
< nums2[j]: newList[k] = nums1[i] i += 1 else: newList[k] = nums2[j] j += 1 if leng %2 == 0: return (newList[tmpLen - 2] + newList[tmpLen - 1])/2 else: return newList[tmpLen - 1] 2.类似与折半查找的思路, 由于题目要求的时间复杂度是(log(m+n)),如果我们直接把两个数组整合一起,那么时间复杂度肯定超过(log(m+n))。所以整理肯定是不行的。那么还有什么方法吗?答案是肯定的。 首先我们要先了解中位数的概念,中位数就是有序数组的中间那个数。那么如果我们将比中位数小的数和比中位数大的数去掉同样的个数,中位数的值也不会变化(数组的个数为偶数的时候另外讨论,因为那时候中位数是中间两个数的平均值,所以中位数旁边两个数不能去掉)。 所以我们不妨试着将数组长度不断缩短。这里不妨提出一个引理。假设有两个有序数组am,bn,他们整合后的有序数组为cn+m。他们的中位数分别是Am/2,Bn/2,C(m+n)/2。如果Am/2 < Bn/2,则 A0…m/2 C(m+n)/2,所以在数组Am里有大于m/2个数大于C(m+n)/2,在数组Bn里也有n/2个数大于C(m+n)/2 也就是说在Cn+m里有(m+n)/2个数大于C(m+n)/2,此时就C(m+n)/2不再是数组Cn+m的中位数。 所以A0…m/2 nums1[size1//2]: return nums1[size1//2] else: return nums2[0] else: if nums2[0] < nums1[size1//2 - 1]: return (nums1[size1//2 - 1] + nums1[size1//2])/2 if nums2[0] >Nums1 [size1//2 + 1]: return (nums1 [size1//2 + 1] + nums1 [size1//2]) / 2 else: return (nums2 [0] + nums1 [size1//2]) / 2 if size1% 2 = = 0: if size2% 2 = = 0: if nums1 [size1//2-1] > Nums2 [size2//2-1] and nums2 [size2//2] > nums1 [size1//2]: return M1 [0] if nums1 [size1//2-1]
< nums2[size2//2 - 1] and nums2[size2//2] < nums1[size1//2]: return m2[0] if m1[0] < m2[0]: return self.findMedianSortedArrays(nums1[m2[1]:],nums2[:size2 - m2[1]]) if m1[0] >M2 [0]: return self.findMedianSortedArrays (nums1 [: size1-m2 [1]], nums2 [m2 [1]:]) else: return M1 [0], the study on "how to find the median of two ordered arrays by Java" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.