1 public class Array { 2 public static int maxSum(int arr[]){ 3 int sum = arr[0]; 4 int b = 0; 5 for(int i=0;i<arr.length;i++){ 6 if(b<=0){ 7 b = arr[i]; 8 }else 9 b+=arr[i]; 10 if(b>sum)sum=b; 11 } 12 return sum; 13 } 14 15 public static void main(String[] args){ 16 int arr[] = {3,7,-1,-8,9,2,5,-9}; 17 int result = maxSum(arr); 18 System.out.println(result); 19 } 20 }
原文:http://www.cnblogs.com/ChinaJun/p/6565544.html