Last active
August 29, 2015 14:04
-
-
Save lijinma/2f8befa4f86b28665dc0 to your computer and use it in GitHub Desktop.
Get max sum of sub array in a array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int maxSumOfSubArray (int * array, int length) | |
{ | |
int sum = 0; | |
int result = 0; | |
for (int index = 0; index < length; index ++) { | |
if (sum + array[index] > array[index]) { | |
sum = sum + array[index]; | |
} | |
else { | |
sum = array[index]; | |
} | |
result = max (result, sum); | |
} | |
return result; | |
} | |
int main () | |
{ | |
//这里你需要使用连续的数组 | |
int a[8] = { 3,4,-4,5,-6,-5,9,4 }; | |
cout << maxSumOfSubArray (a, 8) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment