使用统计学的方法:O(n)
分治的方法,比较复杂
class Solution {
public:
int maxSubArray(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int maxendinghere=A[0];
int max = maxendinghere;
for(int i=1;i<n;i++) {
if (maxendinghere > 0) {
maxendinghere = maxendinghere + A[i];
} else {
maxendinghere = A[i];
}
if (maxendinghere > max) {
max = maxendinghere;
}
}
return max;
}
};