【发布时间】:2019-11-20 13:00:04
【问题描述】:
我有一项工作是在 Ints 数组中获得最高的产品。
给定一个整数数组,找出你能从中得到的最高乘积 三个整数。如果数组大小小于 3,请打印输出 -1
这是我尝试返回的20
func adjacentElementsProduct(inputArray: [Int]) -> Int {
let sorted = inputArray.sorted()
let count = sorted.count
if count > 1 {
return max(sorted[0] * sorted[1] * sorted[2], sorted[count - 1] * sorted[count - 2] * sorted[count - 3])
} else {
return sorted.first ?? 0
}
}
adjacentElementsProduct(inputArray: [3, 1, 2, 5, 4])
这个测试用例怎么会失败
adjacentElementsProduct(inputArray: [1, 10, -5, 1, -100])
它返回 500 并且预期为 5000
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
-
在发布此问题前几秒删除了相同问题的重新发布:stackoverflow.com/questions/58954867/…
标签: swift