Skip to content

Instantly share code, notes, and snippets.

@lijinma
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lijinma/2f8befa4f86b28665dc0 to your computer and use it in GitHub Desktop.
Save lijinma/2f8befa4f86b28665dc0 to your computer and use it in GitHub Desktop.
Get max sum of sub array in a array
#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