博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Code Signal_练习题_adjacentElementsProduct
阅读量:7239 次
发布时间:2019-06-29

本文共 977 字,大约阅读时间需要 3 分钟。

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

Example

For inputArray = [3, 6, -2, -5, 7, 3], the output should be

adjacentElementsProduct(inputArray) = 21.

7 and 3 produce the largest product.

 

我的解答:

1 def adjacentElementsProduct(inputArray): 2     li = [] 3     if len(inputArray) == 1: 4         return inputArray[0] 5     elif len(inputArray) == 2: 6         return inputArray[0]*inputArray[1] 7     else: 8         for i in range(len(inputArray)): 9             if i+2 <= len(inputArray)-1:10                 c_max = max(inputArray[i]*inputArray[i+1],inputArray[i+1]*inputArray[i+2])11                 li.append(c_max)12         return max(li)13 ret = adjacentElementsProduct([3, 6, -2, -5, 7, 3])14 print(ret)

膜拜大神:

def adjacentElementsProduct(inputArray):    return max([inputArray[i] * inputArray[i+1] for i in range(len(inputArray)-1)])
View Code

 

转载于:https://www.cnblogs.com/BlameKidd/p/9343156.html

你可能感兴趣的文章
linux命令行—《命令行快速入门》1
查看>>
设置resin在linux下的默认启动
查看>>
前端面试题
查看>>
Dell R720 安装VMware esx4.1u2
查看>>
SQLServer:镜像监控之oldest unsent transaction告警自愈
查看>>
php写xml文档的一种简单方式
查看>>
Spring使用注解装配之@Autowired
查看>>
oracle数据泵导入导出步骤
查看>>
Scaffold-DbContext
查看>>
职业生涯规划——掌握自己的命运
查看>>
ssh key
查看>>
教训总结
查看>>
sicily 1024 Magic Island
查看>>
一些PHP数组函数介绍
查看>>
docker多容器通过--link互联,如何做到顺序启动?
查看>>
暂存图片下
查看>>
机智云之代码环境搭建和代码烧录
查看>>
Eclipse 常用设置总结
查看>>
linux schedule() 方法浅析
查看>>
Arrays(理解)
查看>>