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. ...
随机推荐
- BitCoin Trading Strategies BackTest With PyAlgoTrade
Written by Khang Nguyen Vo, khangvo88@gmail.com, for the RobustTechHouse blog. Khang is a graduate f ...
- Python 之RabbitMQ使用
1. IO 多路复用 # select 模拟socket server # server 端 import select import socket import sys import queue s ...
- springmvc 之 easyUI开发商城管理系统
1.分页 url:controller的路径 pageSize:每页显示的行数 ---后台参数名(rows) 会向后台传递一个 page参数,表示当前页.---后台参数名(page) controll ...
- PHP json_encode自动转码的问题
用PHP的json_encode处理中文的时候, 中文会被编码成类似于"\u5f20\u4e09"的格式,例如: <?php $arr = array('张三','李四'); ...
- 脚本其实很简单-windows配置核查程序(1)
先上成品图 需求描述 我们电脑上都安装各种过监控软件,比如360.鲁大师等等...其中有一个功能就是性能监控,在安全行业里面通常叫做"配置核查",目的就是将主机的各种性能指标展示, ...
- UVA10341:Solve It(二分+math.h库)
题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68990#problem/E 题目要求:p*e-x+ q*sin(x) + r*co ...
- SpringBoot的核心注解和配置
一.入口类和SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法. @ ...
- Linux系统——VMware克隆
克隆VMware 1. 关闭防火墙 2. 关闭selinux 3. 删除UUID和Mac地址 4.清空网卡缓存 5.关机 ===================== 关闭防火墙 #service ip ...
- Scala快速排序
Scala 快排 Scala 基本思想:经过一趟排序,把待排对象分成两个独立的部分,一部分的数据大(小)于另一部分,同理,对子对象进行如此处理,以达到所有数据都有序. package studen ...
- Tasks in parallel
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...