Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

53. Maximum Subarray #116

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

每次flag都是和sum比较的,所以,sum始终保持着最大值,因为有负数的存在,所以可能会有某个nums[i]大于之前的和 public class Solution { public int maxSubArray(int[] nums) { int sum = nums[0]; int flag = nums[0]; for(int i = 1; i < nums.length; i++) { flag = nums[i] > nums[i]+flag ? nums[i] : nums[i]+flag; if(flag > sum) sum = flag; } return sum; } }

Shawngbk commented 7 years ago

image