首页 > 代码库 > 数组运算

数组运算

public class Maxsum {
public int maxsum(int[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("array is null or empty.");
}


int result = array[0], mark = 0;

for (int i = 0; i < array.length; i++) {
int element = array[i];

if (mark >= 0) {
mark += element;
} else {
mark = element;
}

if (mark > result) {
result = mark;
}
}
return result;
}

public static void main(String[] args) {
Maxsum maxsum = new Maxsum();
int maxSum = maxsum.maxsum(new int[]{-2,2,-5,3,-4});
System.out.println(maxSum);
}

数组运算