Skip to content

Commit f35b54f

Browse files
committed
Create 20. 最大子数组和.md
1 parent 0a9f908 commit f35b54f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

20. 最大子数组和.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
**给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。子数组是数组中的一个连续部分。**
2+
3+
```
4+
class Solution:
5+
def maxSubArray(self, nums: List[int]) -> int:
6+
n = len(nums)
7+
#dp[i]表示以索引i为结尾的连续子数组的最大和
8+
dp = [0]*n
9+
dp[0] = nums[0]
10+
11+
for i in range(1, n):
12+
dp[i] = max(dp[i-1]+nums[i], nums[i])
13+
14+
return max(dp)
15+
```

0 commit comments

Comments
 (0)