原题
https://leetcode.cn/problems/maximum-subarray/description/
思路
动态规划
复杂度
时间:O(n)
空间:O(n)
Python代码
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
# 在索引0处的最大和
cur_max = nums[0]
total_max = cur_max
for i in range(1, len(nums)):
cur_max = max(cur_max + nums[i], nums[i])
total_max = max(total_max, cur_max)
return total_max
Go代码
func maxSubArray(nums []int) int {
// 在索引0处的最大和
cur_max := nums[0]
total_max := cur_max
for i := 1; i < len(nums); i++ {
cur_max = max(cur_max+nums[i], nums[i])
total_max = max(total_max, cur_max)
}
return total_max
}
7780

被折叠的 条评论
为什么被折叠?



