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. ...
随机推荐
- MongoDB 使用 ObjectId 代替时间
An ObjectId is a 12-byte unique identifier consisting of: a 4-byte value representing the seconds si ...
- d3.js:数据可视化利器之 selection:选择集
选择集/selection 选择集/selection是d3中的核心对象,用来封装一组从当前HTML文档中选中的元素: d3提供了两个方法用来创建selection对象: select(selecto ...
- 找新朋友---hdu1286(欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286 欧拉函数:对正整数n,欧拉函数是求少于n的数中与n互质的数的数目: 素数(质数)指在一个大于1的 ...
- Linux文件操作相关命令
1.创建文件夹: [root@izuf6ih01h8fzeziddwkfdz sm]# mkdir a 创建一个名为a的文件夹 2.创建文件: [root@izuf6ih01h8fzeziddwkfd ...
- python包管理一防丢失
pip3 freeze >list.txt 导出当前环境安装的所有包(list是当前项目录下的文件,可以自己命名)pip3 install -r list.txt 安装文件中所 ...
- django views.py视图 获取用户请求相关信息以及请求头
请求的其他信息 用户发来请求时候,不仅发来数据,也把请求头也发过来 在views.py 怎么找请求数据? request是一个对象,这个对象封装很多信息,可以先查这个对象的类 print(type(r ...
- python与c语言交互应用实例
1.python向c语言写数据 1) 先将接收端编译成一个共享链接库gcc/arm-linux-gnueabihf-gcc -o bluetooth_proxy.so -shared -fPIC bl ...
- PAT 1146 Topological Order[难]
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which o ...
- 查看Oracle latch _spin_count默认值
查看Oracle latch _spin_count默认值 SELECT X.KSPPINM NAME, Y.KSPFTCTXVL VALUE, Y.KSPFTCTXDF ISDEFAULT FRO ...
- Django restframwork实现自定义数据格式的分页与搜索
最近因为在做分页时遇到的问题很多,页浪费了好多时间,所以记录一下.以后如遇到可用省去不必要的麻烦 restframwork中的官方文档对分页和搜索页进行了详细的介绍,但是我公司需要的return的js ...