Topic description Hz Occasionally take some professional questions to confuse those non-computer majors. Today, the test team after the meeting, he said again: in the Ancient one-dimensional pattern recognition, it is often necessary to calculate the maximum sum of continuous sub-vectors, when the vector is all positive, the problem is well solved. However, if the vector contains a negative number, should it contain a negative number and expect that the next positive number will compensate for it? For example: {6,-3,-2,7,-15,1,2,2}, the maximum and 8 of continuous sub-vectors (starting from No. 0 to 3rd). Will you be fooled by him? (The length of the sub-vector is at least 1)
1 Public classSolution {2 Public intFindgreatestsumofsubarray (int[] a) {3 intSum =a[0],max=a[0] ;4 for(inti=1;i<a.length;i++){5 if(sum<=0)6sum=A[i]; 7 Else8sum+=A[i]; 9 if(Sum>max) max=sum;Ten } One returnMax; A } -}
30 contiguous sub-arrays of the most Yamato