使用坐标记录下值:i当前子串的开头,j当前子串的结尾
start为最大子串开头,end为最大子串结尾,k为当前的数,ans为最大子串和,sum为当前的和len为最长长度。
import java.util.*;
public class Solution {
public int[] FindGreatestSumOfSubArray (int[] array) {
int i,start,end,k;
int ans,sum,len;
i = k = start = end = 0;
sum = len = 0;
ans = Integer.MIN_VALUE;
while(k<array.length){
sum+=array[k];
if(sum>ans||(sum == ans&&k-i+1>len)){
start = i;
end=k;
ans=sum;
len = k-i+1;
}
if(sum<0){
i=k+1;
sum=0;
}
k++;
}
int arr[] = new int[end-start+1];
int index =0;
for(k = start; k <= end; k++)arr[index++] = array[k];
return arr;
}
}