标签:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
结题报告:
最大连续和,动态规划思想
class Solution { public: int maxSubArray(vector<int>& nums) { int n=nums.size(); if(n==0)return 0; int max=0x80000000; int s=0; for(int i=0;i<n;i++){ if(s+nums[i]<nums[i])s=nums[i]; else s+=nums[i]; if(s>max)max=s; } return max; } }; |
标签:
原文地址:http://www.cnblogs.com/olivelv/p/5263748.html