leetcode刷题系列--152. Maximum Product Subarray
来源:互联网 发布:js获取下拉框选中的值 编辑:程序博客网 时间:2024/06/15 06:16
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4]
,
the contiguous subarray [2,3]
has the largest product = 6
.
Subscribe to see which companies asked this question
Show Similar Problems
Have you met this question in a real interview?
Yes
class Solution {public: int maxProduct(vector<int>& nums) { int len = nums.size(); if(len == 0) return 0; int pos_max = nums[0]; int neg_max = nums[0]; int res_max = nums[0]; for(int i = 1; i < len; ++i) { int a = pos_max * nums[i]; int b = neg_max * nums[i]; pos_max = max(max(a,b),nums[i]); neg_max = min(min(a,b),nums[i]); res_max = max(res_max,pos_max); } return res_max; }};
特殊情况是
[2,-5,-2,-4,3] 如果从头开始遍历 那么是2 -5 -2 变成了20 再弄后面就不对了 其实应该是-2 -4 3 最大值是24
0 0
- leetcode刷题系列--152. Maximum Product Subarray
- LeetCode 152. Maximum Product Subarray
- [leetcode] 152.Maximum Product Subarray
- [LeetCode]152. Maximum Product Subarray
- leetcode:152. Maximum Product Subarray
- leetcode 152. Maximum Product Subarray
- 152. Maximum Product Subarray LeetCode
- [leetcode] 152. Maximum Product Subarray
- LeetCode *** 152. Maximum Product Subarray
- LeetCode 152. Maximum Product Subarray
- leetcode.152. Maximum Product Subarray
- LeetCode-152.Maximum Product Subarray
- LeetCode - 152. Maximum Product Subarray
- [Leetcode]152. Maximum Product Subarray
- LeetCode 152. Maximum Product Subarray
- leetcode:152. Maximum Product Subarray
- [leetcode]152. Maximum Product Subarray
- LeetCode 152. Maximum Product Subarray
- JS中的call()和apply()方法
- eclipse工程右键--->team 没有选项
- VS2013/MFC编程入门之六(对话框:为对话框添加控件)
- 线上课堂目录
- 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
- leetcode刷题系列--152. Maximum Product Subarray
- java并发编程--ConcurrentHashMap、CopyOnWriteArrayList、BlockingQueue
- Android 导入so包后,报java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader
- Android自定义Dialog
- spring-mvc过滤器
- android -日志框架
- ios afnetworking如何往服务器提交图片
- iOS Cocoapods 安装 使用
- Linux中dd命令详解