This is the 11th day of my participation in the August Wenwen Challenge.More challenges in August

Find the median of two positive Ordinal Numbers,

The original: https://leetcode-cn.com/problems/median-of-two-sorted-arrays/

Given two positive-ordered (from small to large) arrays of size m and n, nums1 and nums2. Please find and return the median of the two positive ordinal groups. Example 1: input: nums1 = \[1,3\], nums2 = \[2\] output: 2.00000 explanation: merge array = \[1,2,3\], median 2 example 2: input: nums1 = \[1,2,3\], nums2 = \[3,4\] output: Example 3: input: nums1 = \[0,0\], nums2 = \[0,0\] output: 0.00000 example 4: input: Nums1 = \[\], nums2 = \[1\] Output: 1.00000 Example 5: Input: nums1 = \[2\], nums2 = \[\] Output: 2.00000 Hint:  nums1.length == m nums2.length == n 0 <= m <= 1000 0 <= n <= 1000 1 <= m + n <= 2000 \-10\*\*6 <= nums1\[i\], nums2\[i\] <= 10\*\*6Copy the code

You don’t have to worry about how hard the algorithm is

The front has been updated for several article about the problem of brush LeetCode algorithm, which does not emphasize its difficulty, because the author trying to tested during the process of problem solving, in terms of ease, sometimes the time it takes to solve problems, such as complete a simple and the algorithm of difficult problem, also need to spend 30 minutes, it cannot assess whether difficult really difficult, Simple or simple?Copy the code

To get to the point

1, Pass in two numbers in ascending order (from small to large). (len//2-1)+len//2)/2 (len//2-1 +len//2)/2 (len//2-1 +len//2)/2 (len//2-1 + Len //2)/2 (len//2-1 + Len //2)/2 How to calculate the time complexity of an algorithm problemCopy the code

Their thinking

4. If it is odd, return the index bit of the quotient of the number 5. If it is even, return the sum of the index bit and the elements in the previous position /2Copy the code

It's a little tricky, how do you know what the median of the sum of even length elements is.

Code implementation

class Solution:    
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) - >float:        
        m=len(nums1)        
        nums1[m:]=nums2        
        nums1.sort()        
        d=len(nums1)        
        mid=d%2        
        md=d//2        
        ifmid! =0:            
            return nums1[md]        
        return (nums1[md-1]+nums1[md])/2
Copy the code

The result is as follows:

Knowledge extension

How does Python merge two arrays?

1. Python supports + addition concatenation for iterable data types, such as: STR, list, tuple 2. Python lists support slicing and updating elements. Python lists have built-in append and extend methods, which append an element and append an iterated data typeCopy the code

conclusion

1. Therefore, this problem can be solved by focusing on the characteristics of data types. 2Copy the code