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;
} };

LeetCode——Maximum Product of Three Numbers的更多相关文章

  1. [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积

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

  2. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. LeetCode Maximum Product Subarray(枚举)

    LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...

  4. 628. Maximum Product of Three Numbers@python

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

  5. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...

  6. LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)

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

  7. [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积

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

  8. 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...

  9. [LeetCode&Python] Problem 628. Maximum Product of Three Numbers

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

随机推荐

  1. Elasticsearch 中文分词(elasticsearch-analysis-ik) 安装

    由于elasticsearch基于lucene,所以天然地就多了许多lucene上的中文分词的支持,比如 IK, Paoding, MMSEG4J等lucene中文分词原理上都能在elasticsea ...

  2. Yii框架2.0的视图和widgets表单的使用

    2.0的控制器的创建和action方法的添加和1.0的基本是一样的.但我发现他的widgets 的ActiveForm和1.1版本有些不一样的地方. 对于视图基本也是一样的,有render 和 ren ...

  3. MySQL对指定字段进行加密(双向加密)

    1:建表 test create table test( name varchar(200), value blob ); 插入数据 使用 ENCODE 加密: ,ENCODE('加密字段值', '钥 ...

  4. java-mybaits-00501-案例-映射分析-订单商品数据模型

    1.数据模型分析思路 1.每张表记录的数据内容          分模块对每张表记录的内容进行熟悉,相当 于你学习系统 需求(功能)的过程. 2.每张表重要的字段设置          非空字段.外键 ...

  5. 使用python下载一些链接的软件包

    import reimport requestsimport osimport wget get = raw_input("please input your link::")pa ...

  6. ftp 工作原理

  7. 不受路径限制的 HALCON开发环境, 并且初始化两个Picture控件;

    知识储备: http://bbs.csdn.net/topics/391829463    关于 添加第三方库的方式 http://www.ihalcon.com/read-3730.html  VS ...

  8. The Cheap KD 10 design is not too far of a departure

    Kevin Durant's Cheap KD 10 have to do with to determine the greatest spotlight they have seen around ...

  9. 关于jQuery获得表单radio类型输入框的选中值

    var item=$("input[name='w1']checked").val(); 下面这句,问题解决(加上[type='radio']:): var item=$(&quo ...

  10. Lua的table库函数insert、remove、concat、sort详细介绍(转载)

    函数列表: table.insert(table,[ pos,] value) table.remove(table[, pos]) table.concat(table[, sep[, i[, j] ...