题解 | #连续子数组的最大和#
连续子数组的最大和
https://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484
for_each()打印在类内需要是static函数,确保能够找到Solution::pp
class Solution { public: static void pp(int i){ cout<<i<<" "<<endl; } int FindGreatestSumOfSubArray(vector<int> array) { const int len=array.size(); vector<int> dp(len+1); dp[0]=array[0]; int res=dp[0]; for(int i=1;i<len;i++){ //note array[i] neither dp[i] dp[i]=max(array[i],dp[i-1]+array[i]); res=max(res,dp[i]); } for_each(dp.begin(), dp.end(), Solution::pp); return res; } };