1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) { 4 int result = A[0], sum = A[0]; 5 for (int i = 1; i < n; i++) { 6 sum = max(sum + A[i], A[i]); 7 result = max(result, sum); 8 } 9 return result; 10 } 11 };
时间: 2024-11-06 13:38:28
1 class Solution { 2 public: 3 int maxSubArray(int A[], int n) { 4 int result = A[0], sum = A[0]; 5 for (int i = 1; i < n; i++) { 6 sum = max(sum + A[i], A[i]); 7 result = max(result, sum); 8 } 9 return result; 10 } 11 };