Open youngyangyang04 opened 3 months ago
int sum = Integer.MIN_VALUE, temp = 0;
for (int num : nums) {
temp += num;
if (temp > sum) sum = temp;
if (temp <= 0) temp = 0;
}
return sum;
// 例: nums = [-2,1,-3,4,-1,2,1,-5,4], sum = MIN_VALUE, temp = 0
num = -2: temp = -2, 更新 sum = -2,重计 temp = 0
num = 1: temp = 1, 更新 sum = 1,不重计
num = -3: temp = -2, 不更新,重计 temp = 0
num = 4: temp = 4, 更新 sum = 4,不重计
num = -1: temp = 3, 不更新,不重计
num = 2: temp = 5, 更新 sum = 5,不重计
num = 1: temp = 6, 更新 sum = 6,不重计
num = -5: temp = 1, 不更新,不重计
num = 4: temp = 5, 不更新,不重计
这道题明显更适合用dp,不能为了用
https://www.programmercarl.com/0053.%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C.html