Simple java solution based on mergesort


  • 0
    A

    ...
    class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    int m= nums1.length -1;
    int n= nums2.length-1;
    int[] z = new int[m+n+2];
    for (int k=z.length-1;k>=0;k--){
    if(m<0){
    z[k]=nums2[n--];
    }else if(n<0){
    z[k]=nums1[m--];
    }else if(nums1[m]>nums2[n]){
    z[k]=nums1[m--];
    }else {
    z[k]=nums2[n--];
    }
    }
    if (z.length==1) return z[0];
    if (z.length%2==0){
    return (double)(z[z.length/2] +z[(z.length/2)-1])/2;
    }else{
    return z[z.length/2];
    }
    }
    }
    ...


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.