We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f8e2289 commit 26c6e14Copy full SHA for 26c6e14
leetcode/053.最大子序和.py
@@ -11,11 +11,12 @@ def maxSubArray(self, nums):
11
sum = 0
12
return max_sum
13
14
-# 方法二:动态规划,用数组保存当前最大值
+# 方法二:动态规划,dp[i]表示以下标i元素结尾的数组最大和
15
class Solution(object):
16
def maxSubArray(self, nums):
17
n = len(nums)
18
- max_sum = [nums[0] for _ in range(n)]
+ dp = [nums[0] for _ in range(n)]
19
for i in range(1, n):
20
- max_sum[i] = max(max_sum[i-1] + nums[i], nums[i])
21
- return max(max_sum)
+ # 前一状态最大和加上当前元素值,与当前元素值比较出较大者
+ dp[i] = max(dp[i-1] + nums[i], nums[i])
22
+ return max(dp)
0 commit comments