思路:先令max = nums[0], ans = nums[0]
开始遍历数组,如果当前的ans小于0,则 ans直接 = nums[i]
如果 ans > 0,那么ans 就可以加上当前的值
每循环一次 就判断max(max, ans)最后总能找到最后的最大值
class Solution {
public int maxSubArray(int[] nums) {
int n = nums.length;
if(n == 1 ) return nums[0];
int ans = nums[0];
int max = ans;
for(int i = 1; i< n; i++)
{
if(ans <= 0)
{
ans = nums[i];
}else
{
ans += nums[i];
}
max = Math.max(max, ans);
}
return max;
}
}