LeetCode:Maximum Product Subarray
2016-01-12 15:20
337 查看
Maximum Product Subarray
Total Accepted: 48973 TotalSubmissions: 231867 Difficulty: Medium
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.
Hide Tags
Array Dynamic
Programming
Hide Similar Problems
(M) Maximum Subarray (E)
House Robber (M) Product of Array Except Self
code:
class Solution { public: int maxProduct(vector<int>& nums) { int n = nums.size(); int front=1,back=1; int product = INT_MIN; for(int i=0;i<n;i++) { front *= nums[i]; back *= nums[n-i-1]; product = max(product, max(front, back)); front = front?front:1; back = back?back:1; } return product; } };
相关文章推荐
- opengl基础知识 关于四个变换的顺序
- 设计模式笔记--原型模式
- UIView实现简单触摸拖拽手势
- 收藏一些官方文档
- android服务- Notification创建通知 - startForeground 使用前台服务
- Git查看并修改name和email
- 中国有句古话,叫做日后好相见
- JS的简单交互(web代理)
- android 中动态添加view
- Linux下创建文件系统
- 面试题11:数值的整数次方
- Android中的so挂钩(hook)之Inline hook
- 游戏中资源下载的几点思考
- 非maven项目下载maven的jar
- centos6.5安装nginx
- 浅谈压缩感知(二十五):压缩感知重构算法之分段正交匹配追踪(StOMP)
- rabbitmq学习
- iOS事件传递,响应者链条
- 精简概述何为“数据的完整性”
- 产品经理必读的九步法