一叶之秋lala的gravatar头像
一叶之秋lala 2017-04-26 21:25:18

最长连续子串和的java代码(超简单)

最长连续子串和的java代码(超简单)


public class Test1 {
    public static void main(String[] args) {
        int[] arr = new int[]{1,3,3,-1,-5,3,7,-10};
        int max = arr[0];
        int sum = 0;
        
        for(int i=0; i<arr.length; i++) {
            sum += arr[i];
            
            if(sum < 0) {
                sum = 0;
            }
            
            if(sum > max) {
                max = sum;
            }
        }
        System.out.println("Max:" + max);
    }
}


打赏

文件名:最长连续子串和.zip,文件大小:0.522K 下载
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友