Question

Given an integer array, find three numbers whose product is maximum and output the maximum product.

Example 1:

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

Example 2:

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

Note:

The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].

Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.

Solution

排序,要么最大的三个正数乘积最大,要么是两个最小的负数和最大的正数乘积最大。

Code

class Solution {
public:
int maximumProduct(vector<int>& nums) {
sort(nums.begin(), nums.end(), comp);
return max(nums[0] * nums[1] * nums[2], nums[0] * nums[nums.size() - 1] * nums[nums.size() - 2]);
}
static bool comp(int& a, int& b) {
return a > b;
} };

最新文章

  1. 如何让老Mac机支持USB安装Windows
  2. 20151011 C# 第一篇 运算符
  3. viojs1908无线网路发射器选址
  4. angularJS之事件处理
  5. gif修改背景透明
  6. html背景图片拉伸至全屏
  7. dojo中的xhrPost请求(JSON)
  8. IQueryable &amp; IEnumberable 区别
  9. 饮冰三年-人工智能-linux-03 Linux文件管理(权限管理+归档+压缩)
  10. 两个时间点计算相隔几年,几个月,几天-java
  11. PyTorch(二)Intermediate
  12. springcloud 入门 11 (Hystrix Dashboard)
  13. JavaScript 上万条数据 导出Excel文件(改装版)
  14. MySQL Execution Plan--数据排序操作
  15. Oracle下PLSQL连接没有数据库的问题
  16. [SimpleOJ229]隧道
  17. ZOJ 3964 NIM变形
  18. Python的subprocess模块(一)
  19. sklearn 中模型保存的两种方法
  20. 小程序swiper配置参数使用

热门文章

  1. 全球数字货币交易所TOP20安全性评级报告
  2. Yii框架2.0的 验证码
  3. MapReduce分析流量汇总
  4. centos7虚拟机克隆
  5. opencv 角点检测+相机标定+去畸变+重投影误差计算
  6. HDFS集中式的缓存管理原理与代码剖析
  7. TensorFlow学习笔记(三)MNIST数字识别问题
  8. 20165324 Java实验一
  9. Guid ToString 格式知多少?
  10. iOS 定位方式 iOSNsPredicateString 详解