LeetCode 628. Maximum Product of Three Numbers
题目:
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:
1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
2. Multiplication of any three numbers in the input won’t exceed the range of 32-bit signed integer.
思路:将数组排序,计算第一种情况的最大值,为最后三个数(所有数都是正的,或者只有两个正数,或者只有一个正数时(与第二种情况的相比大小));计算第二种情况的最大值为前两个数和最后一个数的乘积,有负数且绝对值比正数大得多,包含一个正数两个负数,也可能出现最大值。
代码:
class Solution {
public:int maximumProduct(vector<int>& nums) {int len=nums.size();sort(nums.begin(),nums.end());//将数组排序int max1=nums[len-1]*nums[len-2]*nums[len-3];//计算第一种情况的最大值,为最后三个数int max2=nums[0]*nums[1]*nums[len-1];//计算第二种情况的最大值为前两个数和最后一个数的乘积return max1>max2?max1:max2;//返回两个中大的那个}
};
输出结果: 66ms
LeetCode 628. Maximum Product of Three Numbers相关推荐
- leetcode 628. Maximum Product of Three Numbers | 628. 三个数的最大乘积(Java)
题目 https://leetcode-cn.com/problems/maximum-product-of-three-numbers/ 题解 找到三个数的最大乘积.先给数组排序,考虑可能有负数的情 ...
- 628. Maximum Product of Three Numbers
- LeetCode 421. Maximum XOR of Two Numbers in an Array--Python解法
LeetCode 421. Maximum XOR of Two Numbers in an Array–C++,Python解法 LeetCode题解专栏:LeetCode题解 我做的所有的Leet ...
- C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3726 访问. 给定一个整型数组,在数组中找出由三个数组成的最大乘 ...
- LeetCode 152. Maximum Product Subarray--动态规划--C++,Python解法
题目地址:Maximum Product Subarray - LeetCode Given an integer array nums, find the contiguous subarray w ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- LeetCode 152. Maximum Product Subarray
152. Maximum Product Subarray Find the contiguous subarray within an array (containing at least one ...
- [leetcode]421. Maximum XOR of Two Numbers in an Array
421. Maximum XOR of Two Numbers in an Array Given a non-empty array of numbers, a0, a1, a2, - , a
- leetcode 421. Maximum XOR of Two Numbers in an Array
Given a non-empty array of numbers, a0, a1, a2, - , an-1, where 0 ≤ ai < 231. Find the maximum re ...
最新文章
- C语言assert的用法
- 图文并茂——使用xfire编写webservice,并通过C#调用
- php 类的注释标准,php标准注释
- 岑崟:手把手教你走好从技术转管理的第一步
- Atitit.事件机制 与 消息机制的联系与区别
- paip..禁用mmseg 的默认词库. . 仅仅使用自定义词库from数据库.
- HTML 日期格式转换
- 中兴B860AV2.1-T刷机教程加固件
- 针式打印机无电脑测试软件,针式打印机断针测试软件合集
- 亿航白鹭落户西安建丝路总部,为“硬科技之都”插上双翼
- C#读取xml节点数据方法小结
- 工程师思维(简化版)——聪明人的10个工程思维
- 微信小程序——推箱子小游戏
- IOS 视频直播常用集成
- 水晶高跟鞋(测试版)
- 什么牌子的护眼灯对眼睛好?性价比最高的护眼灯
- IBM WebSphere 9.0.5 笔记大全
- 用c++写一个cps测试
- Android禁用返回键
- Spring父子容器详解!!!!
热门文章
- c语言指针如何增加内存大小,C语言如何给指针分配内存?
- thinkpad bios联想logo_最强12吋ThinkPad,X201终极改造:8代酷睿+双内存+NVMe
- OpenCL_Barrier同步
- pytorch1.7教程实验——DCGAN生成对抗网络
- 操作系统(李治军) L12内核级线程的实现
- 基于JavaScript的在线语音识别库Julius
- phpcms搜索功能(案例一)- 代码剥离篇
- dz论坛修改html编辑器,discuz二次开发更换百度ueditor编辑器
- 计算机网络链接的主要目标 主要功能,计算机网络课程教案.doc
- mysql8.0 的新特性_MySQL8.0-新特性汇总