题解 | #连续子数组的最大和(二)#
连续子数组的最大和(二)
https://www.nowcoder.com/practice/11662ff51a714bbd8de809a89c481e21
#include <vector> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param array int整型vector * @return int整型vector */ vector<int> FindGreatestSumOfSubArray(vector<int>& array) { // write code here int n = array.size(); int maxi = -100000,maxi_begin=0, maxi_end = 0; int begin = 0, end = 0; int now_sum = 0; for(int i=0;i<n;i++) { if(array[i]>now_sum+array[i]) { now_sum = array[i]; begin = i; } else { now_sum += array[i]; } if(now_sum>=maxi) { if(now_sum>maxi) { maxi = now_sum; maxi_begin = begin; maxi_end = i; } else if((i-begin)>=(maxi_end-maxi_begin)) { maxi = now_sum; maxi_begin = begin; maxi_end = i; } } } return vector<int>(array.begin()+maxi_begin,array.begin()+maxi_end+1); } };