题解 | #子数组的最大累加和问题#
子数组的最大累加和问题
http://www.nowcoder.com/practice/554aa508dd5d4fefbf0f86e5fe953abd
class Solution { public: /** * max sum of the subarray * @param arr int整型vector the array * @return int整型 */ int maxsumofSubarray(vector<int>& arr) { int sum=0,num=0; for(int i=0; i<arr.size(); i++) { num+=arr[i]; if(num<=0) num=0; sum=max(sum,num); } return sum; } };
当累加和小于0时,将累加和重置为0