class Solution { public int maxSubArray(int[] nums) { //动态规划 int len = nums.length; int[] res = new int[len]; int max = nums[0]; res[0] = nums[0]; if(len==1) return max; for(int i = 1;i<len;i++){ if(res[i-1]<=0){ res[i] = nums[i]; }else{ res[i] = nums[i] + res[i-1]; } max = Math.max(max,res[i]); } return max; } }
动态规划