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 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- Content-Type实体首部字段
现代互联网下,每天都会有数以亿计的各种媒体对象经由HTTP传输,如图像,文本,影视以及软件程序等.这些数据都包含在HTTP报文的实体内容中,如果把HTTP报文想像成一份快递,HTTP实体就是快递实 ...
- 去除VS2010中中文注释下的红色波浪线
1,中文注释以分号结尾 2,Assist X 菜单栏->Assist X Option->Underline 选择“min”.
- 黑马程序员—C语言的特点和关键字
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- C语言的简介 一. C语言具有下列特点: C语言既具有低级语言直接操纵硬件的特点,又具有高级语言 ...
- [HDOJ 5155] Harry And Magic Box
题目链接:HDOJ - 5155 题目大意 有一个 n * m 的棋盘,已知每行每列都至少有一个棋子,求可能有多少种不同的棋子分布情况.答案对一个大素数取模. 题目分析 算法1: 使用容斥原理与递推. ...
- delphi 程序是否为控制台编译选项
http://www.birdol.com/article/tag/delphi 编译指令写在哪?: 编译指令可以写在代码页的任何地方, 不过在代码的不同区域有时也会不同; 譬如: {$APPTYPE ...
- iostat 使用说明
LINUX [oracle@perass back]$ iostat -m 1 10 Linux 2.6.18-194.el5 (perass) 03/01/2014 avg-cpu: %user % ...
- The Same Game(模拟)
http://poj.org/problem?id=1027 题意:给一个10*15的地图,里面填充R,G,B三种颜色,每次找到当前地图的同色最大区域M,并将其删除,删除M后,上面的小球自然下落,当有 ...
- CodeAssistant
软件名:CodeAssistant 很霸气的名字,不过目前仅有的功能是代码格式化. 用途: 在向大神请教时,不妨用这小软件把自己的代码格一下.我们的口号就是让大神看得舒心,让BUG无处遁形. 演示: ...
- hg 证书验证失败
hg clone https://bitbucket.org/pygame/pygame 出现abort: error: _ssl.c:504: error:14090086:SSL routines ...
- 南桥先生谈《OUTLIERS》
借来一套语音版的 Outliers 听完了.这本书里有很多故事,可是希望借此找到成功的奥秘恐怕很难,作者做的是一描述而不是预见.听了半天,只听出了六个字: “天时地利人和”. 比如比尔·盖茨,他之所以 ...