典型的动态规划题,这道题我采用从后向前的思想找最大值
动态规划方程式就是代码的为代码
public class Solution {
public int maxSubArray(int[] nums) {
int[] addA = new int[nums.length];
int max = 0,i = nums.length - 1,temp = 0;
addA[i] = nums[i];
max = addA[i];
i--;
while(i>=0){
temp = addA[i+1] + nums[i];
if(nums[i] > 0 && addA[i+1]>0){
addA[i] = temp;
}else if(nums[i]<= 0 && addA[i+1]>0 &&temp>0)
addA[i] = temp;
else
addA[i] = nums[i];
if(addA[i] > max){
max = addA[i];
}
i--;
}
return max;
}
}
2016/8/30