Java code O(m+n)


  • 0
    R
    public class Solution {
        public double findMedianSortedArrays(int[] nums1, int[] nums2) {
            int total_length=nums1.length+nums2.length;
            double median=0;
            int mid=(total_length-1)/2;
            boolean isodd=(total_length%2==1);
            int i=0;
            int j=0;
            double e1=0;
            double e2=0;;
            for(int k=0;k<total_length;k++){
                int e;
                if(i>nums1.length-1){
                    
                    e=nums2[j];
                    j++;
                }
                else if(j>nums2.length-1){
                   
                    e=nums1[i];
                     i++;
                }
                else if(nums1[i]>nums2[j]){
                    
                    e=nums2[j];
                    j++;
                }
                else{
                    
                    e=nums1[i];
                    i++;
                }
                if(k==mid){
                    e1=e;
                }
                if(k==(mid+1)){
                    e2=e;
                }
            }
            if(isodd){
               return e1; 
            }    
            return (e1+e2)/2;
        }
    }

Log in to reply
 

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