题目
给定一个数组,其中元素可正可负,求其中最大连续子序列的和。
代码
public class FindGreatestSum {
public static void main(String[] args) {
int[] array = {6,-3,-2,7,-15,1,2,2};
System.out.println(FindGreatestSumOfSubArray(array));
}
public static int FindGreatestSumOfSubArray(int[] array) {
if (array == null || array.length <= 0){
return 0;
}
int temp = array[0];
int num = array[0];
for (int i = 1; i < array.length; i++){
if (temp >= 0){
temp+=array[i];
}else{
temp = array[i];
}
if (temp > num){
num = temp;
}
}
return num;
}
}