simple javascript solution combines the 2 arrays into 1 sorted array & returns length/2 value


  • 0
    C
    /**
     * @param {number[]} nums1
     * @param {number[]} nums2
     * @return {number}
     */
    var findMedianSortedArrays = function(nums1, nums2) {
        var total = [];
        while (nums1.length > 0 || nums2.length > 0) {
        	if (nums1.length > 0 && nums2.length > 0) {
        		if (nums1[0] < nums2[0]) {
        			total.push(nums1.shift())
        		} else {
        			total.push(nums2.shift())
        		}
        	} else if (nums1.length > 0) {
        		total.push(nums1.shift())
        	} else {
        		total.push(nums2.shift())
        	}
        }
        if (total.length % 2 != 0) {
        	return total[Math.floor(total.length / 2)];
        } else {
        	return (total[Math.floor(total.length / 2)-1] + total[Math.floor(total.length / 2)]) / 2
        }
    };
    

Log in to reply
 

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