1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Solution { public : int FindGreatestSumOfSubArray (vector < int > array) { if (array .empty ()) return 0; int sum =0; int max =-99999999; for ( int i=0;i<array .size ();i++) { sum +=array[ i]; if (sum-array [i ]<0 &&sum >0) sum =array [i ]; else if (sum<0 ) sum =array [i ]; if (sum>max )max =sum ; } return max; } }; |
全输入负数序列也能返回最大的负数值