![](https://www.dazhuanlan.com/webchat.jpg)
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include<limits.h> #include<vector> using namespace std;
class { public: int maxSubArray(vector<int>& nums) { int ans = 0, maxn = INT_MIN; int len = nums.size(); for(int i = 0; i < len; i++){ if(ans < 0) ans = 0; ans += nums[i]; maxn = max(maxn, ans); } return maxn; } }; int main(){ Solution s; int A[]={-2,1,-3,4,-1,2,1,-5,4}; vector<int> B(A,A+9); cout<<s.maxSubArray(B); }
|
近期评论