Hi, this is my accepted JS solution.


  • 0
    T
    /**
     * @param {number[]} nums1
     * @param {number[]} nums2
     * @return {number}
     */
    var findMedianSortedArrays = function(nums1, nums2) {
        var merged = null;
        if(!nums1) {
            merged = nums2;
        }
        if(!nums2) {
            merged = nums1;
        }
        if(nums1.length <= nums2.lenght) {
            for(var i=0; i<nums1.length; i++) {
                nums2.push(nums1[i])
            }
            merged = nums2;
        } else {
            for(var i=0; i<nums2.length; i++) {
                nums1.push(nums2[i])
            }
            merged = nums1;
        }
        var ogg = merged.length%2;
        var middle = Math.floor(merged.length / 2)
        merged.sort(function(a,b){ return b-a });
        if(!ogg) {
            return (merged[middle] + merged[middle-1])/2
        } else {
            return merged[middle]
        }
    };
    

Log in to reply
 

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