最大子序列和算法

最大子序列和,数组中从k到m的子序列的最大和问题

public void maxSum() {
        int a[] = {1, 2, 5, 3, -15, 8};

        int thisNum = 0;
        int maxNum = 0;
        for (int i = 0; i < a.length; i++) {
            thisNum += a[i];
            if (thisNum > maxNum) {
                maxNum = thisNum;
            } else if (thisNum < 0) {
                thisNum = 0;
            }
        }
        System.out.println("maxNum:"+maxNum);
    }

点赞