Array

package com.example.liuy.maxsubarray;

import java.util.ArrayList;
import java.util.List;

import static android.R.id.list;

/**
* Created by liuY on 2017/3/17.
*/

public class MaxSubArray {
public static int maxSum(int arr[]) {
int sum = arr[0];
int b = 0;
for (int i = 0; i < arr.length; i++) {
if (b <= 0) {
b = arr[i];
} else
b += arr[i];
if (b > sum) sum = b;
}
return sum;
}

public static void main(String[] args) {

int arr[] = {3, 7, -1, -8, 9, 2, 5, -9};
int result = maxSum(arr);
System.out.println(result);
}
}


原文地址:https://www.cnblogs.com/u1118746/p/6565502.html