JAVA最长子序列

来源:互联网 发布:知乎日报打不开 编辑:程序博客网 时间:2024/06/06 16:55
public static void main(String[] args) {        int[] num = { 1, 1, 1, 1, -4, -4, -4, -3, 4, 8, 6 };        int maxSum = 0;//定义最大数值        int thisSum = 0;//定义获取相加的数值        int begin = 0, start;//定义获得开始的索引        start = begin;        int end, finash = 0;//定义获得结束的索引        end = finash;        for (int i = 0; i < num.length; i++) {            finash = i;            thisSum += num[i];            if (thisSum > maxSum) {                maxSum = thisSum;                end = finash;//记录相加之后的索引位置                start = begin;            }             if (thisSum < 0) {                thisSum = 0;                begin = i + 1;            }        }        System.out.println(maxSum);        System.out.print("开始索引为"+start+"结束索引"+end);    }