这道题求的是乘积的最大值的,那么依照之前的和的最大值类似的做法的,乘积的最大值可能是在最大值*当前值和最小值*当前值和当前值三者之间取得的最大值的,那么使用两个变量来保存每一步的最大最小值的。

class Solution {
public:int maxProduct(vector<int>& nums) {if(nums.size()<=0) return 0;int maxn,minn,res;maxn=minn=res=nums[0];for(int i=1;i<nums.size();i++){if(nums[i]>0){maxn=max(maxn*nums[i],nums[i]);minn=min(minn*nums[i],nums[i]);}else{int t=maxn; // 注意这里改变了最大值的maxn=max(minn*nums[i],nums[i]);minn=min(t*nums[i],nums[i]);}res=max(maxn,res);}return res;}
};

从上面看出还可以更简短代码一些的,当小于0的时候就可以直接进行交换最大和最小值的,也不需要利用中间变量来重新替换的。

class Solution {
public:int maxProduct(vector<int>& nums) {if(nums.size()<=0) return 0;int maxn,minn,res;maxn=minn=res=nums[0];for(int i=1;i<nums.size();i++){if(nums[i]<0) swap(maxn,minn);maxn=max(maxn*nums[i],nums[i]);minn=min(minn*nums[i],nums[i]);res=max(maxn,res);}return res;}
};

转载于:https://www.cnblogs.com/newnoobbird/p/9629053.html

leetcode 152. Maximum Product Subarry相关推荐

  1. LeetCode 152. Maximum Product Subarray

    152. Maximum Product Subarray Find the contiguous subarray within an array (containing at least one ...

  2. LeetCode 152. Maximum Product Subarray--动态规划--C++,Python解法

    题目地址:Maximum Product Subarray - LeetCode Given an integer array nums, find the contiguous subarray w ...

  3. leetcode 152. Maximum Product Subarray | 152. 乘积最大子数组(Java)

    题目 https://leetcode.com/problems/maximum-product-subarray/ 题解 class Solution {public int maxProduct( ...

  4. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  5. LeetCode 628. Maximum Product of Three Numbers

    题目: Given an integer array, find three numbers whose product is maximum and output the maximum produ ...

  6. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  7. Leetcode 2233. Maximum Product After K Increments

    题目 解法1 首先理解一下题意,要求的是对数组中某些数字增加1后的最大乘积,一共可增加k次.对于每一次增加1,整个乘积的变化是除了被增加这个数字以外的数字总和.那么最后乘积最大也就是k次增加的最多.从 ...

  8. leetcode 1339. Maximum Product of Splitted Binary Tree | 1339. 分裂二叉树的最大乘积(树形dp)

    题目 https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/ 题解 树形 dp,思路见草稿 /*** Defini ...

  9. leetcode 318. Maximum Product of Word Lengths | 318. 最大单词长度乘积

    题目 https://leetcode.com/problems/maximum-product-of-word-lengths/ 题解 中规中矩的题目,中规中矩的思路,不像是一个 medium 题. ...

最新文章

  1. JSON.parse解析特殊字符报错解决方案
  2. 潜移默化学会WPF(转载篇二)--退出应用程序
  3. 从零入门 FreeRTOS 操作系统之任务调度器
  4. Linux报错./configure: error: C compiler cc is not found
  5. 使用python进行数据清洗常用的库_python3常用的数据清洗方法(小结)
  6. 2020 年 4 月编程语言排行榜:C 语言重回王者?
  7. [css] 说说display:none和visibility:hidden的区别
  8. 【前端自动化构建】之 自动化部署
  9. Java 集合系列12之 Hashtable详细介绍(源码解析)和使用示例
  10. Java开发Maven插件
  11. (10)数据分析-变量分析
  12. 【笔记+总结】Andrew Ng-神经网络和深度学习
  13. android 修复工具,牛学长安卓手机修复工具(安卓手机修复助手)V2.4.0.11 免费版
  14. 论文阅读笔记 | 分类网络——ParNet
  15. Flutter 内外边距
  16. conda命令:管理包、管理环境
  17. 计算机网络个人简历范文,计算机网络个人简历范文
  18. 微信小程序自定义组件数据、方法和属性
  19. mpc5125的lpc总线
  20. 在ArcGIS中批量删除属性表字段

热门文章

  1. 中国大学MOOC 计算机组成原理第1章测试
  2. Java 设计模式 --- 单例模式
  3. Linux 内核中的 cdev_alloc和cdev_add
  4. 数据结构与算法之美01-开篇词
  5. 大四报了个天价培训班,变成量产型炮灰工程师
  6. Buuoj reverse1
  7. 迅雷启动不了,提示Atl71.dll文件丢失
  8. 2440启动文件分析
  9. Codeforces Round #506 (Div. 3) - E. Tree with Small Distances
  10. VS Code运行Python程序