markdown C# - JS - Python - 算法:相邻元素产品

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown C# - JS - Python - 算法:相邻元素产品相关的知识,希望对你有一定的参考价值。

int adjacentElementsProduct(int[] inputArray) {
    //The Select method changes each element in the result
    // i is the element in the array and j is the current index
    // If j is greater than 0, take the value of i * inputArray[j-1], i * (value in the previous position)
    // If j es less or equal to 0, take the value of int.MinValue (-2147483648)
    // From the resulting IEnumerable, return the item with the max value
    return inputArray.Select((i, j) => j > 0 ? i * inputArray[j-1] : int.MinValue).Max();
}
function adjacentElementsProduct(inputArray) {
  // ... - This is the spread operator, and it essentially takes either an array or an object and expands it into its set of items.
  // map() – returns a new list with the result of each item in an array
  return Math.max(...inputArray.slice(1).map((x,i)=>[x*inputArray[i]]));
}
def adjacentElementsProduct(inputArray):
    return max([inputArray[i] * inputArray[i+1] for i in range(len(inputArray)-1)]) 
# Description:
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

以上是关于markdown C# - JS - Python - 算法:相邻元素产品的主要内容,如果未能解决你的问题,请参考以下文章

markdown C# - JS - Python - 算法:从一年开始的世纪

前端学习必备精选文章60篇:JS正则表达式markdown语法前端面试

vue使用marked.js实现markdown转html并提取标题生成目录

博客园里Markdown支持高亮显示的语言

Vue3.0中读取本地Markdown文件的内容

js 数组中如何删除字典