首页 > 编程语言 > 详细

数组运算

时间:2017-03-17 14:57:16      阅读:165      评论:0      收藏:0      [点我收藏+]
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);
}

数组运算

原文:http://www.cnblogs.com/GJD28/p/6565719.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!