标签:
1 class Solution { 2 public: 3 int FindGreatestSumOfSubArray(vector<int> array) { 4 int MAX = -999999; 5 int last = -1; 6 int len = array.size(); 7 if (len == 0) 8 return 0; 9 for(int i = 0 ; i < len; ++i) 10 { 11 if(MAX < array[i]) 12 { 13 MAX = array[i]; 14 } 15 } 16 for (int i = 0 ;i < len ; ++ i) 17 { 18 if(array[i] > 0) 19 { 20 if(last < 0) 21 { 22 last = 0; 23 MAX = array[0]; 24 } 25 else 26 { 27 if(last == i-1) 28 { 29 last = i; 30 MAX += array[i]; 31 } 32 else 33 { 34 int sum = 0; 35 for (int k = last + 1;k <= i;++k) 36 { 37 sum += array[k]; 38 } 39 if (sum > 0 && MAX + sum >= array[i]) 40 { 41 MAX += sum ; 42 last = i; 43 } 44 if (MAX < array[i]) 45 { 46 MAX = array[i]; 47 last = i; 48 } 49 } 50 } 51 } 52 } 53 return MAX; 54 } 55 };
标签:
原文地址:http://www.cnblogs.com/xiaoyesoso/p/5158462.html