public class Solution { /** * @param nums: A list of integers * @return: A integer indicate the sum of max subarray */ public int maxSubArray(int[] nums) { int n = nums.length; int ans = -1000000; int sum = 0; for(int i=0; i<n; i++) { sum += nums[i]; if(sum > ans) { ans = sum; } if(sum < 0) { sum = 0; //子串和为负数,丢掉 } } return ans; } }