Share my O(log(m+n)) solution with Java, using CONTINUE


  • 0
    K
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    		int n = nums1.length + nums2.length;
    		int[] a = new int[n];
    		for (int i = 0, j = 0, k = 0; k < n; k++) {
    			if (i == nums1.length && j < nums2.length) {
    				a[k] = nums2[j++];
    				continue;
    			} else if (j == nums2.length && i < nums1.length) {
    				a[k] = nums1[i++];
    				continue;
    			}
    			if(nums1[i] < nums2[j])
    				a[k] = nums1[i++];
    			else 
    				a[k] = nums2[j++];
    		}
    		double median = 0;
    		if (n % 2 == 1)
    			median = a[n/2];
    		else 
    			median = 1.0 * (a[n/2] + a[n/2-1]) / 2;
    		return median;
        }

Log in to reply
 

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