动态规划:最大连续子序列和

问题:给出一个数组,求其连续子序列的最大和

package 动态规划;

/**
 * 给出一个数组,求其连续子数组的最大和
 * @author Administrator
 *
 */
public class MaxSum {

    public static void main(String[] args) {
        
        int[] arr = new int[]{-3,1,-3,4,-1,2,1};
        int max=arr[0];
        int current=arr[0];
        for(int i=1;i<arr.length;i++){
        if(current<0){
        current = arr[i];
        }else{
        current += arr[i];
        }
        if(current>max) max=current;
        }
        System.out.println(max);
    }            

}
原文地址:https://www.cnblogs.com/ChanSS/p/6715785.html