大和
package 最大数组子和;
public class FindMaxSumOfSubArray {
public class FindMaxSumOfSubArray {
/**
* @param args
*/
public static void main(String[] args) {
FindMaxSumOfSubArray f = new FindMaxSumOfSubArray();
int[] arr = { 1, -2, 3, 10, -4, 7, 2, -5 };
System.out.println("MaxSum:" + f.findMaxSum(arr));
}
* @param args
*/
public static void main(String[] args) {
FindMaxSumOfSubArray f = new FindMaxSumOfSubArray();
int[] arr = { 1, -2, 3, 10, -4, 7, 2, -5 };
System.out.println("MaxSum:" + f.findMaxSum(arr));
}
public Integer findMaxSum(int[] arr) {
int curSum = 0;
int maxSum = 0;
int len = arr.length;
int curSum = 0;
int maxSum = 0;
int len = arr.length;
if (arr == null || len == 0) {
return null;
}
return null;
}
for (int i = 0; i < len; i++) {
curSum += arr[i];
if (curSum < 0) {
curSum = 0;
}
if (curSum > maxSum) {
maxSum = curSum;
}
}
curSum += arr[i];
if (curSum < 0) {
curSum = 0;
}
if (curSum > maxSum) {
maxSum = curSum;
}
}
// all data are negative
if (maxSum == 0) {
for (int i = 0; i < len; i++) {
if (i == 0) {
maxSum = arr[i];
}
if (arr[i] > maxSum) {
maxSum = arr[i];
}
}
}
return maxSum;
}
}
if (maxSum == 0) {
for (int i = 0; i < len; i++) {
if (i == 0) {
maxSum = arr[i];
}
if (arr[i] > maxSum) {
maxSum = arr[i];
}
}
}
return maxSum;
}
}