博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
628. Maximum Product of Three Numbers
阅读量:4499 次
发布时间:2019-06-08

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


 

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:

  1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
  2. Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.

 

找出3个数,使他们的乘积最大,数字会有负数

 

C++(79ms):

1 class Solution { 2 public: 3     int maximumProduct(vector
& nums) { 4 sort(nums.begin(),nums.end()) ; 5 int len = nums.size() ; 6 int a = nums[0]*nums[1]*nums[len-1] ; 7 int b = nums[len-1]*nums[len-2]*nums[len-3] ; 8 return a>b?a:b ; 9 }10 };

 

 

转载于:https://www.cnblogs.com/mengchunchen/p/7125832.html

你可能感兴趣的文章
hdoj-1276-士兵队列训练问题(队列模拟)
查看>>
CentOS7升级OpenSSL版本
查看>>
PHP-函数
查看>>
微软Azure AspNetCore微服务实战 第一期
查看>>
命令行调用dubbo远程服务
查看>>
virtualbox 创建com对象失败
查看>>
编写 jQruy 插件 框架
查看>>
JavaScript常用获取宽高的方法
查看>>
开发Web Service的几种方式
查看>>
Dynamics 365 CRM 添加自定义按钮
查看>>
缺陷概述
查看>>
使用Eclipse对FFmpeg进行调试
查看>>
R语言数据类型
查看>>
hdu3267 Graph Game 缩点 + 博弈
查看>>
java 自定义异常类
查看>>
小橙书阅读指南(七)——优先队列和索引优先队列
查看>>
例行报告
查看>>
mysql操作类库--摘抄
查看>>
oracle 用户管理(一)
查看>>
QT下自定义QQ聊天窗口tab控件
查看>>