LeetCode *** 152. Maximum Product Subarray
2016-04-25 14:58
411 查看
题目:
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array
the contiguous subarray
分析:
代码:
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.
分析:
代码:
class Solution { public: int maxProduct(vector<int>& nums) { int size = nums.size(), res = INT_MIN; if (size == 1)return nums[0]; vector<int> neg(size + 1, INT_MAX), pos(size + 1, INT_MIN); neg[0] = pos[0] = 1; for (int i = 0; i<size; ++i) { neg[i + 1] = min(neg[i] * nums[i], pos[i] * nums[i]); neg[i + 1] = min(neg[i + 1], nums[i]); pos[i + 1] = max(neg[i] * nums[i], pos[i] * nums[i]); pos[i + 1] = max(pos[i + 1], nums[i]); res = max(neg[i + 1], res); res = max(pos[i + 1], res); } return res; } };
相关文章推荐
- java路径改不过来的原因和解决办法
- android自定义view----等分饼图,实现每个块中间的间隔
- 浏览器兼容
- 最小生成树之Prim算法--蓝白点思想
- 九、oracle 事务
- node.js WebService异常处理(domain)以及利用domain实现request生命周期的全局变量
- git中忽略UserInterfaceState.xcuserstate的方法
- 同步与异步、阻塞与非阻塞
- spring的事务解读
- Linux系统下adb devices无法看到移动设备
- 编译原理--C-Minus词法分析器C++实现
- Android关于OnTouch 和OnClick同时调用冲突的解决方案
- DbUtils组件
- 使用cocoaPods 做依赖的管理
- 翻译:谷歌HTML、CSS和JavaScript风格规范
- block和代理
- 天天学C#--数据访问类(一)
- [改善Java代码] 推荐使用序列化实现对象的拷贝
- 状态码
- 重温MVC基础入门