码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-15 23:11:30      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:ring   个数   code   public   动态规划   序列   new   current   连续   

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

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);
    }            

}

 

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

标签:ring   个数   code   public   动态规划   序列   new   current   连续   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!