problem

628. Maximum Product of Three Numbers

题意:三个数乘积的最大值;

solution1:

如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其绝对值就该最小,而负数排序后绝对值小的都在末尾,所以是末尾三个数字相乘,这个跟全是正数的情况一样。那么重点在于前半段是负数,后半段是正数,那么最好的情况肯定是两个最小的负数相乘得到一个正数,然后跟一个最大的正数相乘,这样得到的肯定是最大的数,所以我们让前两个数相乘,再和数组的最后一个数字相乘,就可以得到这种情况下的最大的乘积.

class Solution {
public:
int maximumProduct(vector<int>& nums) {
//positive-negtive;
sort(nums.begin(), nums.end());
int n = nums.size();
int tmp = nums[n-]*nums[n-]*nums[n-];
return max(tmp, nums[]*nums[]*nums[n-]);
}
};

solution2:找出三个最大的或者一个最大的两个最小的,比较他们的乘积,而且是O(n)的时间复杂度

注意,求解数值时候的逻辑顺序。

class Solution {
public:
int maximumProduct(vector<int>& nums) {
//positive-negtive;
int max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN;
int min1 = INT_MAX, min2 = INT_MAX;
for (auto num : nums)
{
if(num>max1)
{
max3 = max2; max2 = max1; max1 = num;
}
else if(num>max2)
{
max3 = max2; max2 = num;
}
else if(num>max3) max3 = num; if(num<min1)
{
min2 = min1; min1 = num;
}
else if(num<min2) min2 = num;
}
return max(max1*max2*max3, max1*min1*min2);
}
};

参考

1. Leetcode_easy_628. Maximum Product of Three Numbers;

2. Grandyang;

【Leetcode_easy】628. Maximum Product of Three Numbers的更多相关文章

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

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

  2. 628. Maximum Product of Three Numbers@python

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

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

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

  4. 628. Maximum Product of Three Numbers

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

  5. [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. ...

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

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

  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. [Array]628. Maximum Product of Three Numbers

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

  9. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

随机推荐

  1. 34、[源码]-AOP原理-链式调用通知方法

    34.[源码]-AOP原理-链式调用通知方法

  2. 005_simulink建立条件子系统

    1. 条件执行子系统 a)  使能子系统:是控制信号大于零时执行的子系统.在控制信号穿越零点由负变正的时步点上,使能子系统开始执行.只要子系统的控制信号保持正值,使能子系统就会保持在执行的状态 b)  ...

  3. java后台防止XSS的脚本攻击

    import java.util.regex.Pattern; //具体过滤关键字符public class XSSUtil { private static Pattern[] patterns = ...

  4. PostgreSQL10.5 - 创建索引的思考

    总体感觉整个索引创建的比较慢,PostgreSQL10没有并行创建索引的功能,所以执行过程中,仅用到了服务器的一个核心来执行计算.索引创建是一个高CPU消耗的工作,CPU基本会跑满,会用到backen ...

  5. JSON.parseObject的几种用法

    https://blog.csdn.net/a18827547638/article/details/80272099 https://blog.csdn.net/a18827547638/artic ...

  6. python中如何给散点图上面的特定点做标记

    今天想在散点图的某些特定的点外面画圆圈标记,从下面的文章找到一些灵感,只要在原来的散点图上面给指点添加相应的标志,设置其透明度就可以实现该想法. 顺便复习下散点图的用法. 大家平时为了直观地显示数据的 ...

  7. chrome的内存限制

    推荐阅读:https://www.cnblogs.com/chengxs/p/10919311.html chrome内存限制 存在限制 Chrome限制了所能使用的内存极限(64位为1.4GB,32 ...

  8. python生成二维码(简易)

    首先要的配置: pillow image qrcode zxing 然后直接上代码: import PIL import qrcode # 实例化二维码生成类 qr = qrcode.QRCode( ...

  9. (转)服务端监控工具:nmon的使用

    在性能测试过程中,对服务端的各项资源使用情况进行监控是很重要的一环.这篇博客,介绍下服务端监控工具:nmon的使用方法.. 一.认识nmon 1.简介 nmon是一种在AIX与各种Linux操作系统上 ...

  10. ArrayUtils.

    String sfck=mp.get("SFCK")==null?"":mp.get("SFCK").toString();     Str ...