Leetcode- find the median of two positively ordered arrays

4. Find the median of two positive arrays

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

Given two positive (small to large) arrays nums1 and nums2 of size m and n. Please find and return the median of these two positive arrays.

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

Example 1:

Input: nums1 = [1,3], nums2 = [2]
Output: 2.0000
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

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

prompt:

nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
Passes 268,312 Submissions 691,163

Insert picture description here

The code is a bit long, and the idea is very simple;
ideas:
1. If nums1 is empty, then only need to find the median of the second array
2. If nums2 is empty, then only need to find the median of the first array
3. If they are not empty, merge nums1 and nums2, and then find the median of the merged array

package 其他.每日一题;

import java.util.Arrays;

class Solution {
    
    

    static  public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    
    

        if(nums1.length==0)
            return findMidArrays(nums2);
        if(nums2.length==0)
            return findMidArrays(nums1);

        int[] hebing = hebing(nums1, nums2);
//        System.out.println("合并好的数组:"+Arrays.toString(hebing));
        return findMidArrays(hebing);
    }

    //合并数组
    private static int[] hebing(int[] nums1, int[] nums2){
    
    
        int len1 = nums1.length;
        int len2 = nums2.length;
        int[] newIntArray = new int[len1+len2];
        int newIndex = 0;

        int i = 0;
        int j = 0;

        while (len1!=0 && len2!=0){
    
    

            if(nums1[i]>nums2[j]){
    
    
                newIntArray[newIndex]=nums2[j];
                len2--;
                j++;
            }else{
    
    
                newIntArray[newIndex]=nums1[i];
                len1--;
                i++;
            }
            newIndex++;
        }

//        System.out.println(Arrays.toString(newIntArray));

        //合并剩余的
        if(len1!=0){
    
    
            for (int k = i; k < nums1.length; k++) {
    
    
                newIntArray[newIndex]=nums1[k];
                newIndex++;
            }
        }

        if(len2!=0){
    
    
            for (int k = j; k < nums2.length; k++) {
    
    
                newIntArray[newIndex]=nums2[k];
                newIndex++;
            }
        }
//        System.out.println(Arrays.toString(newIntArray));

        return newIntArray;
    }

    //查找一个单独的数组中的中位数
    private static double findMidArrays(int[] nums) {
    
    
        if(nums.length==2){
    
    
            return nums[0]+nums[1]==0?0:(nums[0]+nums[1]) / 2.0;
        }

       double midNumber = 0;

//        int number = (nums.length-1)/2;
        int numsLen = nums.length;

        //有偶数个
        if(numsLen%2==0){
    
    
            int len = numsLen / 2;

            midNumber = (nums[len] + nums[len-1]) / 2.0;

        //有奇数个
        }else{
    
    
            numsLen = (nums.length/2)+1;
            midNumber = nums[numsLen-1];
        }

        return midNumber;
    }


    public static void main(String[] args) {
    
    
        System.out.println(findMedianSortedArrays(new int[]{
    
    1,2,3,4,5,6},new int[]{
    
    }));
        System.out.println(findMedianSortedArrays(new int[]{
    
    1,3},new int[]{
    
    2}));
    }

}


Guess you like

Origin blog.csdn.net/qq_17623363/article/details/108945861