solution.md 1.6 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
# 最大子序和

<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>

## template

```python
class Solution(object):
	def maxSubArray(self, nums):
		maxEndingHere = maxSofFar = nums[0]
		for i in range(1, len(nums)):
			maxEndingHere = max(maxEndingHere + nums[i], nums[i])
			maxSofFar = max(maxEndingHere, maxSofFar)
		return maxSofFar
# %%
s = Solution()
print(s.maxSubArray(nums = [-2,1,-3,4,-1,2,1,-5,4]))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```