LeetCode——Maximum Product of Three Numbers
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的更多相关文章
- [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode Maximum Product Subarray(枚举)
LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, ...
- 628. Maximum Product of Three Numbers@python
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 【Leetcode_easy】628. Maximum Product of Three Numbers
problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
- LeetCode 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 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- [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. ...
随机推荐
- 2017 Multi-University Training Contest - Team 1—HDU6033&&HDU6034
HDU6033 Add More Zero 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6033 题目意思:给一个m,求一个数k使得10^k最接近2 ...
- 【opencv】 solvepnp 和 solvepnpRansac 求解 【空间三维坐标系 到 图像二维坐标系】的 三维旋转R 和 三维平移 T 【opencv2使用solvepnp求解rt不准的问题】
参考: pnp问题 与 solvepnp函数:https://www.jianshu.com/p/b97406d8833c 对图片进行二维仿射变换cv2.warpAffine() or 对图片进行二维 ...
- Python并行编程(十):多线程性能评估
1.基本概念 GIL是CPython解释器引入的锁,GIL在解释器层面阻止了真正的并行运行.解释器在执行任何线程之前,必须等待当前正在运行的线程释放GIL,事实上,解释器会强迫想要运行的线程必须拿到G ...
- 【云安全与同态加密_调研分析(6)】云计算及云安全主流体系架构与模型——By Me
云计算及云安全的主流体系架构与模型 1. 云计算主流安全参考模型 1.1 云计算安全参考 ...
- oracle入门(6)——PL/SQL常用语法
[本文介绍] 本文不是”语法大全“,只是记录下作项目里自己常用的一些语法.方便查询. [语法] [输出] (1)输出语法 DBMS_OUTPUT.PUT_LINE( ) [定义] (1)定义变 ...
- py-统计一个矩阵中每一列的非0数的个数
1.文件类型类似于这样: 不过数据量比这个要更大一点. 2.对应上述数据的运行结果: import matplotlib.pyplot as plt with open('test.txt') as ...
- TensorFlow学习笔记(六)循环神经网络
一.循环神经网络简介 循环神经网络的主要用途是处理和预测序列数据.循环神经网络刻画了一个序列当前的输出与之前信息的关系.从网络结构上,循环神经网络会记忆之前的信息,并利用之前的信息影响后面节点的输出. ...
- Zen cart 根据数量打折插件
Quantity Discounts 可以根据顾客购买多少来打折,很不错. 假如顾客买了3个以上的产品,就给他10%折扣,设置如下: Turn On Quantity Discount 1. In t ...
- PHP实现excel导出
首先去下载PHPExcel类,地址http://phpexcel.codeplex.com/ 方法如下第一步引入这个扩展类 Vendor('PHPExcel'); 第二部就是方法了,下面简单的实现方法 ...
- [C语言]小记q = (++j) + (++j) + (++j)的值
根据不同的编译器,生产的代码不一样,导致的结果也会不一样. 代码如下: #include <stdio.h> void main() { ; int q; q =(++j)+(++j)+( ...