0
点赞
收藏
分享

微信扫一扫

视觉SLAM理论与实践的学习链接汇总

IT程序员 03-29 08:00 阅读 4

注意:子数组是数组中的一个连续部分

示例1

示例2

public class MaximumSbuarray {
    public static void main(String[] args) {
        int[] nums = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
        int pre = nums[0];
        int max = nums[0];
        int start = 0;
        int end = 0;
        int startMax = 0;
        int endMax = 0;
        for (int i = 1; i < nums.length; i++) {
            int curNum = nums[i];
            //往前走,不断确定序列区间
            if (pre + curNum > curNum) {
                end = i;
                pre += curNum;
            } else {
                start = i;
                end = i;
                pre = curNum;
            }
            //确定最大值
            if (pre > max) {
                startMax = start;
                endMax = end;
                max = pre;
            }
        }
        System.out.println("statrMax=" + startMax + "," + "endMax=" + endMax);
        System.out.println("max=" + max);
    }
}
举报

相关推荐

0 条评论