标签:
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
.
public class Solution { public int maxProduct(int[] nums) { int[] pos=new int[nums.length]; int[] neg=new int[nums.length]; pos[0]=nums[0]; neg[0]=nums[0]; int max=nums[0]; for(int i=1;i<nums.length;i++) { pos[i]=Math.max(Math.max(neg[i-1]*nums[i],pos[i-1]*nums[i]),nums[i]); neg[i]=Math.min(Math.min(neg[i-1]*nums[i],pos[i-1]*nums[i]),nums[i]); max=Math.max(pos[i],max); } return max; } }
标签:
原文地址:http://www.cnblogs.com/Machelsky/p/5894779.html