package collection;
public class HashMapDemo01 {
public static void main(String args[]) {
System.out.println(solve(new int[] { 1, -3, 3, 10, -4, 7, 2, -5 }));
}
public static long solve(int[] a) {
int len = a.length;
if (len == 0) {
return 0;
}
int currentSum = a[0];
int greatsetSum = a[0];
for (int i = 1; i < a.length; i++) {
if (currentSum > 0) {
currentSum += a[i];
} else {
currentSum = a[i];
}
if (currentSum > greatsetSum) {
greatsetSum = currentSum;
}
}
return greatsetSum;
}
}
【数据结构】连续子数组的最大和
最新推荐文章于 2024-05-24 18:24:27 发布