152.[LeetCode] Maximum Product Subarray
2024-08-31 12:28:58
Given an integer array nums
, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
Input: [2,3,-2,4]
Output:6
Explanation: [2,3] has the largest product 6.
Example 2:
Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution {
public:
int maxProduct(vector<int>& nums) {
int ans=nums[],frontprod=,backprod=;
int n=nums.size();
for(int i=;i<n;i++){
frontprod*=nums[i];
backprod*=nums[n-i-];
ans=max(ans,max(frontprod,backprod));
frontprod=frontprod == ? :frontprod;
backprod=backprod == ? : backprod; }
return ans;
}
};
最新文章
- 95、Jenkins部署.net持续集成自动化测试环境
- Codeforces Round #339 (Div.2)
- 查看Exchange邮件队列(queue)
- PHP5 mysqli 教程
- Git ~ 添加远程仓库 ~Git
- HDU 1023 Train Problem II (卡特兰数,经典)
- sql server2012 动态端口
- DJANGO:根据不同的环境,配置不同的SETTINGS文件,读取不同的DB,JENKINS,SALT配置
- Xcode的控制台调试命令
- Java程序员面试题集(51-70)(转)
- 关于URL编码的问题
- RNTools
- CHD4 impala安装配置
- zzw原创_ipv6下环境配置防火墙及FTP处理一例
- Tesseract
- Servlet-转发和重定向的区别
- the import XXXX cannot be resolved 解决方法
- VMware网络桥接模式与NAT模式共存
- 【VB.NET】利用 ZXing.Net 生成二维码(支持自定义LOGO)
- LeetCode-206.ReverseLinked List