剑指offer——30.连续子数组的最大和
问题描述:
{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和
思路:
使用动态规划
直接对原数组进行迭代式修改:
代码:
import java.lang.Math; public class Solution { public int FindGreatestSumOfSubArray(int[] array) { int res = nums[0]; for(int i = 1; i < nums.length; i++) { nums[i] += Math.max(nums[i - 1], 0); res = Math.max(res, nums[i]); } return res; } }