【Leetcode_easy】628. Maximum Product of Three Numbers
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的更多相关文章
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- 628. Maximum Product of Three Numbers@python
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [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. ...
- LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)
题目: Given an integer array, find three numbers whose product is maximum and output the maximum produ ...
- [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [Array]628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 【LeetCode】152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
随机推荐
- Python 操作Zabbix API 获取ERROR级别告警信息并打印
1.需求:有一个语音合成播报项目,要实时获取zabbix的ERROR级别以上告警信息,将该信息合成语音播报出去.(合成语音及播报已经完成) 2.现实:整理zabbix告警级别,将不太重要的告警放到ER ...
- 树莓派远程桌面--使用windows自带的远程桌面软件mstsc.exe
步骤: 1.使用SSH登录树莓派 2.安装xrdp服务: sudo apt-get install xrdp 3.在Windows下打开命令框运行:mstsc 4.登录树莓派桌面 5.登录成功
- 003_创建simulink文件
001_创建simulink文件 1. 打开MATLAB,打开simulink 2. 打开空白模块 3. 保存,并打开模块的选择 4. 在模块里面选择后拖出模块后连线 或在搜索名称后拖出来 或在空白的 ...
- Dao的扩展
题目: 1.查询所有学生记录,包含年级名称2.查询S1年级下的学生记录 一.项目目录 package com.myschool.entity; import java.util.ArrayList; ...
- Oracle 11g 禁用 SQL Tuning Advisor 与 auto space advisor
生产上有一套11g数据库alert.log报错ORA-16957: SQL Analyze time limit interrupt. 查询MOS相关文档Troubleshooting: ORA-1 ...
- AdvanceEast源码理解
目录 文章思路 源码理解 一. 标签点形式 按顺序排列四个点,逆时针旋转,且第一个点为左上角点(刚开始选择最左边的点, 二. 标签切边 三. loss计算 四. NMS 最后说明 文章思路 大神的gi ...
- python下载后出现python 已停止工作
背景: 在执行IDLE或者在terminal窗口执行 python命令时出现如下提示,修改了防火墙关闭也不行,找不到解决办法? 如图: [解决方案] 1.卸载重装python,确保python版本与系 ...
- cas系列-自定义异常提示(五)
cas对于异常提示可以支持多语言,默认是英语,具体文件格式大同小异.这里以cas5.x版本为例. 官方推荐使用overlay方式部署,这样你只需要关注你自定义的配置即可. 获取自定义配置文件方式: 执 ...
- java继承内存分配
java继承内存分配 继承的基本概念: * Java不支持多继承,也就是说子类至多只能有一个父类. * 子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法. * 子类中定义的成员 ...
- mysql插入数据自动生成主键uuid
DemoMapper.java //注意方法的返回值必须是void; void add(Demo demo); ============================================ ...