My Java program , easy to understand!


  • 0
    K
    public class Solution {
        public double findMedianSortedArrays(int[] nums1, int[] nums2) {
       	int len = nums1.length+nums2.length;
    	if(len==0){
    		System.out.println("invalid input!");
    		return 0.0;
    	}
    	double mid = 0.0;
    	if(len%2==0){
    		mid = (findMid(nums1,0,nums2,0,len/2)+findMid(nums1,0,nums2,0,len/2+1))/2;
    	}else{
    		mid= findMid(nums1,0,nums2,0,len/2+1);
    	}
    	return mid;
    
    }
    public static double findMid( int[] nums1,int start1,int[] nums2,int start2,int midPos){
    	int curr=0;
    	int i=0,j=0;
    	if(nums1.length==0){
    		return nums2[midPos-1];
    	}
    	if(nums2.length==0){
    		return nums1[midPos-1];
    	}
    	for(int k =0;k<midPos;k++){
    		if(i<nums1.length&&j<nums2.length){
    			if(nums1[i]<nums2[j]){
    				curr=nums1[i++];
    			}else{
    				curr=nums2[j++];
    			}
    		}else if(i>=nums1.length&&j<nums2.length){
    			curr=nums2[j++];
    		}else{
    			curr=nums1[i++];
    		}
    	}
    	return curr;
    	
    }
    }

  • -3
    J

    Just so you know, it doesn't work!


  • 0
    N

    Could you be more specific?


  • 0
    L

    the question is asking a log time runtime. yours is linear


  • 0
    J

    It works, I must have pasted it in some other problem. My bad, apologies.


Log in to reply
 

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