Public classZhejiang { Public Static voidMain (string[] args) {int[] arr=New int[] {1,-2,3,10,-4,7,2,-5}; intI=getgreatestsum (arr); System.out.println (i); } Private Static intGetgreatestsum (int[] arr) { if(arr==NULL|| Arr.length==0) { return0; } intGreatestsum=0; inttemp=greatestsum; for(inti = 0; i < arr.length; i++) {Temp+=Arr[i]; if(temp<0{//if current and is negative, discard this value directly, should have positive number in array element, select any integer greater than negative temp=0; } if(temp>greatestsum) {//Discover new Max and reassign it to Greatestsum greatestsum=temp; } } if(greatestsum==0{//Because many values are discarded, this extreme situation occurs, so the maximum value in a single element is finally assigned to Greatestsum Greatestsum=arr[0]; for(inti = 1; i < arr.length; i++) { if(greatestsum<Arr[i]) {Greatestsum=Arr[i]; } } } returngreatestsum; }}
Enter an array of shapes with positive and negative elements, one or more contiguous elements as their sub-arrays, and a maximum of all sub-arrays