#GESP4282. [GESP]L5-202609-Q4282-S

[GESP]L5-202609-Q4282-S

下面函数使用迭代方法求最大连续子段和。对于数组 {-2, 3, -1, 5, -6, 2},函数返回值是( )。

int maxSubArray(const vector<int> &a) {
    int best = a[0];
    int current = a[0];
    for (int i = 1; i < (int)a.size(); i++) {
        current = max(a[i], current + a[i]);
        best = max(best, current);
    }
    return best;
}

{{ select(1) }}

  • 5
  • 6
  • 7
  • 8