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 .
Analysis: This topic is the maximum value that allows you to find the product of a contiguous subarray.
(1) In the case that there is no 0 in the array, the maximum value is either nums[0]+nums[1]+ .... +nums[i] or nums[j]+nums[j+1]+ .... +nums[n-1].
When the array is full of integers, or even a number of negative numbers, the answer is nums[0]+nums[1]+ .... +NUMS[N-1]
(2) There are 0 cases in the array, assuming nums[i]=0. The answer should be from nums[0]+nums[1]+ .... +NUMS[I-1] or nums[i+1]+nums[i+2]+ .... +NUMS[N-1] (assuming that there is only one zero case, as long as there are n zeros, the array is divided into n+1 sub-arrays can be)
The code is as follows:
Public classSolution { Public intMaxproduct (int[] nums) { intmax=int. Minvalue,front=1, back=1; for(intI=0; I<nums. length;i++) {Front*=Nums[i]; back*=nums[nums. length-i-1]; Max=Math.max (Max,math.max (front,back)); Front=front==0?1: Front; back=back==0?1: Back; } returnMax; }}
C # solution Leetcode 152. Maximum Product Subarray