C#解leetcode 152. Maximum Product 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
.
分析:这个题目就是让你求连续的子数组的乘积的最大值。
(1)在数组中没有0的情况下,最大值要么是nums[0]+nums[1]+。。。。+nums[i]或者是nums[j]+nums[j+1]+。。。。+nums[n-1].
当数组中全是整数的时候,或者又偶数个负数的时候,答案为nums[0]+nums[1]+。。。。+nums[n-1]
(2)在数组中有0个情况下,假设nums[i]=0.则答案应该从nums[0]+nums[1]+。。。。+nums[i-1]或者是nums[i+1]+nums[i+2]+。。。。+nums[n-1]中寻找(假设只有一个零的情况,至于当有n个零的时候,将数组分成n+1个子数组就可以了)
代码如下:
public class Solution {
public int MaxProduct(int[] nums) {
int max= int.MinValue,front=,back=; for(int i=;i<nums.Length;i++)
{
front*=nums[i];
back*=nums[nums.Length-i-]; max=Math.Max(max,Math.Max(front,back)); front=front==?:front;
back=back==?:back;
} return max;
}
}
C#解leetcode 152. Maximum Product Subarray的更多相关文章
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- LeetCode 152. Maximum Product Subarray (最大乘积子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Java for LeetCode 152 Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 152. Maximum Product Subarray --------- java
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]152. Maximum Product Subarray最大乘积子数组
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- Leetcode#152 Maximum Product Subarray
原题地址 简单动态规划,跟最大子串和类似. 一维状态空间可以经过压缩变成常数空间. 代码: int maxProduct(int A[], int n) { ) ; ]; ]; ]; ; i > ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- 152. Maximum Product Subarray - LeetCode
Question 152. Maximum Product Subarray Solution 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- openerp import l field size limit
modify the file addons/base_import/models.py add the following line at the very begining of the _con ...
- 跨平台的zip文件压缩处理,支持压缩解压文件夹
根据minizip改写的模块,需要zlib支持 输出的接口: #define RG_ZIP_FILE_REPLACE 0 #define RG_ZIP_FILE_APPEND 1 //压缩文件夹目录, ...
- MongoDB主从配置
master的配置 # cat mongod.conf dbpath = /app/sinova/mongodata/db #指定数据库目录 logpath = /app/sin ...
- (转载)Mysql使用Describe命令判断字段是否存在
(转载)http://www.jz123.cn/plus/view.php?aid=39200 工作时需要取得MySQL中一个表的字段是否存在 于是就使用Describe命令来判断 mysql_con ...
- 【模拟】NEERC15 G Generators(2015-2016 ACM-ICPC)(Codeforces GYM 100851)
题目链接: http://codeforces.com/gym/100851 题目大意: n个序列.每个序列有4个值x,a,b,c,之后按照x=(a*x+b)%c扩展无穷项. 求每个序列各取一个数之后 ...
- SQL中的NULL值
除is [not] null之外,空值不满足任何查找条件.–如果null参与算术运算,则该算术表达式的值为null.–如果null参与比较运算,则结果可视为false.在SQL-92中可看成unkno ...
- FreeMarker-TemplateLoader
Java中不乏优秀的模板引擎,Velocity,mvel,FreeMarker等.在构建框架的时候,通常可以拿来即用,但我们需要控制它.最近需要一个数据准备的框架,便选择了FreeMarker,Fre ...
- poj3294 Life Forms(后缀数组)
[题目链接] http://poj.org/problem?id=3294 [题意] 多个字符串求出现超过R次的最长公共子串. [思路] 二分+划分height,判定一个组中是否包含不小于R个不同字符 ...
- 通过web修改svn用户密码
使用方法: 将文件changePasswd.cgi和changePasswd.ini 放到apche安装目录下的cgi-bin下(cgi-bin的目录可以通过/etc/httpd/conf/httpd ...
- rpm常用操作
1.查询.检查软件包 rpm {-q|--query} [select-options] [query-options] rpm {-V|--verify} [select-options] [ver ...