image.png

解题思路

  1. public int maxSubArray(int[] nums) {
  2. if(nums==null||nums.length==0)
  3. return 0;
  4. int maxRes = Integer.MIN_VALUE;
  5. int sum = 0;
  6. for(int val:nums){
  7. if(sum<=0)
  8. sum = val;
  9. else
  10. sum += val;
  11. maxRes = Math.max(maxRes, sum);
  12. }
  13. return maxRes;
  14. }