标签:code col 子串 ash stat amp subarray pre tor
给定数列 nums
dp[i] ——以nums[i]为结尾的子串的最大和 ***
开始:dp[0]=nums[0]
状态转移:dp[i] = max( dp[i-1]+nums[i],nums[i] )
class Solution { public: static const int INF = 0x7fffffff; int maxSubArray(vector<int>& nums) { int len = nums.size(); int maxn = INF*(-1); vector<int> dp(len); if (nums.size() == 0)return 0; for (int i = 0;i < len;i++) { if (i == 0) dp[0] = nums[0]; else dp[i] = max(nums[i] + dp[i - 1], nums[i]); if (dp[i] > maxn)maxn = dp[i]; } return maxn; } };
标签:code col 子串 ash stat amp subarray pre tor
原文地址:https://www.cnblogs.com/suuusu/p/10986497.html