- 遍历数组,进行求和,更新当前连续子数组结果;
- 如果数组和小于0,重新开始累加;
- 遍历完成,返回最终结果。
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array) {
int count = 0;
int result = INT_MIN;
for (int i = 0; i < array.size(); i++) {
count += array[i];
if (result < count) result = count;
if (count < 0) count = 0;
}
return result;
}
};