我们可以使用以下代码跟踪最大子数组:
import java.util.Arrays;
public class KadaneSolution4MaxSubArray{
public static void main(String[]args){
int [] array = new int[]{13,-3,-25,20 ,-3 ,-16,-23,18,20,-7,12,-5,-22,15,-4,7};
int[] maxSubArray = maxSubArrayUsingKadaneSol(array);
for(int e : maxSubArray){
System.out.print(e+"\t");
}
System.out.println();
}
public static int[] maxSubArrayUsingKadaneSol(int array[]){
long maxSoFar =array[0];
long maxEndingHere =array[0];
int startIndex = 0;
int endIndex =0;
int j=1;
for(; j< array.length ;j++){
int val = array[j];
if(val >= val+maxEndingHere){
maxEndingHere = val;
startIndex = j;
}else {
maxEndingHere += val;
};
if(maxSoFar < maxEndingHere){
maxSoFar = maxEndingHere;
endIndex = j;
}
}
return Arrays.copyOfRange(array,startIndex,endIndex+1);
}
}
PS假设给定数组是最大子数组问题的候选者,并且所有元素都不是负数