当前位置:网站首页>Maximum product of three numbers in question 628 of Li Kou

Maximum product of three numbers in question 628 of Li Kou

2022-07-06 22:04:00 Big chicken legs are best

628. The maximum product of three numbers

The difficulty is simple 353 Switch to English to receive dynamic feedback

Here's an integer array  nums , Find the largest product of three numbers in the array , And output this product .

Example 1:

 Input :nums = [1,2,3]
 Output :6

Example 2:

 Input :nums = [1,2,3,4]
 Output :24

Example 3:

 Input :nums = [-1,-2,-3]
 Output :-6

/**

 * @param {number[]} nums

 * @return {number}

 */

var maximumProduct = function(nums) {

   nums=nums.sort((a,b)=> {return a-b})

   let len=nums.length-1

   let l=nums[0]*nums[1]*nums[len]

   let p=nums[len-2]*nums[len-1]*nums[len]

   let c=Math.max(l,p)

   return c

       

};

原网站

版权声明
本文为[Big chicken legs are best]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131107090656.html