Skip to content

Commit 26c6e14

Browse files
authored
Update 053.最大子序和.py
1 parent f8e2289 commit 26c6e14

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

leetcode/053.最大子序和.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ def maxSubArray(self, nums):
1111
sum = 0
1212
return max_sum
1313

14-
# 方法二:动态规划,用数组保存当前最大值
14+
# 方法二:动态规划,dp[i]表示以下标i元素结尾的数组最大和
1515
class Solution(object):
1616
def maxSubArray(self, nums):
1717
n = len(nums)
18-
max_sum = [nums[0] for _ in range(n)]
18+
dp = [nums[0] for _ in range(n)]
1919
for i in range(1, n):
20-
max_sum[i] = max(max_sum[i-1] + nums[i], nums[i])
21-
return max(max_sum)
20+
# 前一状态最大和加上当前元素值,与当前元素值比较出较大者
21+
dp[i] = max(dp[i-1] + nums[i], nums[i])
22+
return max(dp)

0 commit comments

Comments
 (0)