solution.md 3.6 KB
Newer Older
1
# 最大子序和
F
fix bug  
feilong 已提交
2

3
<p>给定一个整数数组 <code>nums</code> ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [-2,1,-3,4,-1,2,1,-5,4]<strong><br />输出:</strong>6<strong><br />解释:</strong>连续子数组 [4,-1,2,1] 的和最大,为 6 。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>nums = [1]<strong><br />输出:</strong>1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>nums = [0]<strong><br />输出:</strong>0</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>nums = [-1]<strong><br />输出:</strong>-1</pre><p><strong>示例 5:</strong></p><pre><strong>输入:</strong>nums = [-100000]<strong><br />输出:</strong>-100000</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>	<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li></ul><p> </p><p><strong>进阶:</strong>如果你已经实现复杂度为 <code>O(n)</code> 的解法,尝试使用更为精妙的 <strong>分治法</strong> 求解。</p>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29
```cpp
int main()
{
    Solution sol;
    vector<int> nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
    int res;

    res = sol.maxSubArray(nums);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
```cpp
class Solution
{
public:
    int maxSubArray(vector<int> &nums)
    {
        if (nums.empty())
            return 0;
        int pre = 0, res = nums[0];
        for (auto c : nums)
        {
            pre = max(pre, c);
            res = max(pre, res);
        }
        return res;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
51

52
### A
F
fix bug  
feilong 已提交
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
```cpp
class Solution
{
public:
    int maxSubArray(vector<int> &nums)
    {
        int sum = 0, max_sum = INT_MIN;
        for (int i = 0; i < nums.size(); i++)
        {
            if (sum < 0)
            {
                sum = nums[i];
            }
            else
            {
                sum += nums[i];
            }
            max_sum = max(sum, max_sum);
        }
        return max_sum;
    }
};
```

### B
F
fix bug  
feilong 已提交
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
```cpp
class Solution
{
public:
    int maxSubArray(vector<int> &nums)
    {
        if (nums.size() == 0)
            return NULL;
        return fenzhifa(nums, 0, nums.size() - 1);
    }
    int fenzhifa(vector<int> &nums, int left, int right)
    {
        if (left > right)
            return INT_MIN;
        if (left == right)
            return nums[left];
        int mid = (left + right) / 2;
        int l = fenzhifa(nums, 0, mid - 1);
        int r = fenzhifa(nums, mid + 1, right);
        int t = nums[mid];
        int max_num = nums[mid];
        for (int i = mid - 1; i >= left; i--)
        {
            t += nums[i];
            max_num = max(max_num, t);
        }
        t = max_num;
        for (int i = mid + 1; i <= right; i++)
        {
            t += nums[i];
            max_num = max(max_num, t);
        }
        return max(max(r, l), max_num);
    }
};
```

### C
F
fix bug  
feilong 已提交
118

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
```cpp
class Solution
{
public:
    int maxSubArray(vector<int> &nums)
    {
        int maxsum = nums[0];
        for (int i = 0; i < nums.size(); i++)
        {
            int sum = 0;
            for (int j = i; j < nums.size(); j++)
            {
                sum = sum + nums[j];
                if (maxsum < sum)
                    maxsum = sum;
            }
        }
        return maxsum;
    }
};
```