求最大子串和以及其中一个子串(java)
public static void getMaxSum(int[] a){
int max = a[0];
int sum = a[0];
int temp = 0;
int start = 0;
int end = 0;
for(int i = 1;i<a.length;i++){
if(sum>0){
sum +=a[i] ;
}
else{
sum = a[i];
temp = i;
}
if(sum>max){
max = sum;
start = temp;
end = i;
}
}
for(int i = start ; i<=end;i++){
print(a[i]+" ");
}
print("Max="+max);
}