JavaScript Solution short and simple


  • 0
    Z
    /**
     * @param {number[]} nums
     * @return {number}
     */
    var maximumGap = function(nums) {
        if(nums ===null||nums.length < 2) return 0;
        if(nums.length == 2) return (nums[0]>nums[1])?nums[0]-nums[1]:nums[1]-nums[0];
        nums.sort(function(a,b){return a - b});
        var tmp = nums[1]-nums[0], max = tmp;
        for(var x = 1 ; x< nums.length;x++){
            tmp = nums[x]-nums[x-1];
            max = (max>tmp)? max:tmp;
        }
        return max;
    };
    

Log in to reply
 

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