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 题目大意:求数列中连续子序列的最大连乘积 思路:动态规划实现,现在动态规划理解的还不透,照着公式往上套的 ...
随机推荐
- pecl安装php的ev扩展时的报错处理
pecl安装php的ev扩展,安装完毕后php.ini中加入扩展extension=ev.so,然后重启php-fpm出现以下报错 PHP Warning: PHP Startup: Unable ...
- google yeoman
Yeoman是Google的团队和外部贡献者团队合作开发的,他的目标是通过Grunt(一个用于开发任务自动化的命令行工具)和Bower(一个HTML.CSS.Javascript和图片等前端资源的包管 ...
- Unity3d 合作开发项目
Unity3d 合作开发项目 交流群:63438968 本人:灰太龙 项目的合作开发是至关重要的,第一个问题就是自适应分辨率的问题! 综合考虑了一下,我们采用了IGUI插件,这个插件有以下几 ...
- 13. vs2010 ClientID bug处理
在VS2010中的产生ClientID有几种方式,每个控件或页面有个ClientIDMode属性,可以用来决定产生ClientID的方式,它有AutoID,Static,Inherit,Predict ...
- mysql 监控长事务
mysql> desc information_schema.innodb_trx -> ; +----------------------------+----------------- ...
- 数位DP:SPOJ KPSUM - The Sum
KPSUM - The Sum One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that ...
- arcgis安装问题SDK开始安装不了
arcgis安装问题SDK开始安装不了,提示安装Desktop10,安装Desktop10又提示装dotnetfx3.5sp1,最后其实不用这样,可以先把军事扩展包,补丁,安装好,再安装sdk就没有问 ...
- floyd详解
就不在来回搬了 请看 http://note.youdao.com/groupshare/?token=6422E952998F4381A1534B71359EFA57&gid=1579165 ...
- Searching the String - ZOJ 3228(ac自动机)
题目大意:首先给你一下母串,长度不超过10^5,然后有 N(10^5) 次查询,每次查询有两种命令,0或者1,然后加一个子串,询问母串里面有多少个子串,0表示可以重复,1表示不可以重复. 分析:发 ...
- 高频交易:Solarflare组建超低延迟网络
10Gb以太网适配器制.网卡造商Solarflare目前正在将自己的网卡系列产品转变为服务器产品.其产品在金融领域有着广泛的应用. Solarflare首先将现场可编程门阵列(FPGA)放入网络适配器 ...