One day to the algorithm question - find the median of two positive sequence arrays

Title
Given two positive order (from small to large) arrays nums1 and nums2 of sizes m and n. Please find and return the median of these two ordinal arrays.

Advanced: Can you design an algorithm with time complexity O(log (m+n)) to solve this problem?

Source: LeetCode
Link: https://leetcode-cn.com/problems/median-of-two-sorted-arrays

Example 1:
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: Combined array = [1,2,3], median 2

Example 2:
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: Combined array = [1,2,3,4], median (2 + 3) / 2 = 2.5

For this question, the first thing that comes to mind is to combine the two arrays first, and then find the median. Then the time complexity is mainly in the merger of the two arrays. The following is my scumbag violent solution idea: (array index starts from 0)

input int[] nums1, int[]nums2, int []nums
output int med
1. The length of nums1 and nums2 is not 0, go to 2; the length of nums1 is 0, go to 3; the length of nums2 is 0, go to to 4;
2. Circularly compare each element in nums1 and nums2, and put the smaller one into nums; when nums1 is traversed, put the remaining elements in nums2 directly into nums; when nums2 is traversed , put the remaining elements in nums1 directly into nums; go to 5;
3.nums=nums2; go to 5;
4.nums=nums1; go to 5;
5. Determine whether the length of nums is odd, if it is odd, Then the median is nums.length/2; if even, the median is (nums.length/2+nums.length/2-1)/2.

The following is my slag implementation:

 /*
    *思路:首先将两个有序数组合为1个数组。然后找出数组的中位数。
    *如何合并?两个数组存在几种情况:1.两个数组都不为null的情况;2.nums1数组为null的情况
    *3.nums2为空的情况;4.两个数组都为null的情况。
    *对于1:遍历两个数组,比较两个数组中元素的大小,将其中元素小的放入新的数组;当有一个数组遍历完毕时,将
    *       另一个数组直接加入新的数组;
    *对于2:直接将nums2数组元素放到新的数组中;
    *对于3:直接将nums1数组元素放到新的数组中;
    *对于4:返回0;
    *如何找出中位数?这个就很简单啦。
    */
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    
    
       int length1=nums1.length;
       int length2=nums2.length;
       int [] nums=new int[length1+length2];
        int k=0;
       if(length1!=0&&length2!=0){
    
    
           int i=0;
           int j=0;
           while(i<length1&&j<length2){
    
    
               if(nums1[i]<=nums2[j])
                   nums[k++]=nums1[i++];
               else
                    nums[k++]=nums2[j++];
           }
           if(i<length1) {
    
    
               while(i<length1)
                   nums[k++]=nums1[i++];
           }
           if(j<length2) {
    
    
               while(j<length2)
                  nums[k++]=nums2[j++];
           }
            int length=nums.length;
           if(length%2==0)
               return ((double)nums[length/2]+(double)nums[length/2-1])/2;
           else
                return nums[length/2];

       }

       if(length1==0){
    
    
           nums=nums2;
            int length=nums.length;
           if(length%2==0)
               return ((double)nums[length/2]+(double)nums[length/2-1])/2;
           else
                return nums[length/2];

       }
       if(length2==0){
    
    
           nums=nums1;
            int length=nums.length;
           if(length%2==0)
               return ((double)nums[length/2]+(double)nums[length/2-1])/2;
           else
                return nums[length/2];

       }
       return 0.0d;
    }

Guess you like

Origin blog.csdn.net/weixin_43763175/article/details/109633472