Find the contiguous subarray within an array (containing at least one number) which have the largest product.
For example, given the array [2,3,-2,4] ,
The contiguous Subarray has the [2,3] largest product = 6 .
Public classSolution { Public intMaxproduct (int[] nums) { //because the subject is the product of the largest, it is necessary to maintain the current maximum and current minimum value, and in nums[i]**,curmax*nums[i],curmin*nums[i] can be taken//variable res is required to hold the largest result in the middle. if(nums.length<1)return0; intCurmax=nums[0]; intCurmin=nums[0]; intMax=nums[0]; intRes=nums[0]; for(inti=1;i<nums.length;i++){ inttemp=curmax*Nums[i]; intttemp=curmin*Nums[i]; Curmax=Math.max (Nums[i],math.max (temp,ttemp)); Curmin=math.min (Nums[i],math.min (ttemp,temp)); Res=Math.max (Res,curmax); } returnRes; }}
[Leedcode] Maximum Product subarray