学习时间:
2023年1月28日
题目描述:

题解分享:
# 作者: 繁华倾夏
# 2023年01月28日
# 力扣(LeetCode):53. 最大子数组和
class Solution:
# def maxSubArray(self, nums: List[int]) -> int: # 力扣测试时使用
def maxSubArray(nums): # pycharm测试时使用
length = len(nums) # 计算列表(数组)长度
pre = 0
remax = nums[0]
for i in range(length): # 遍历
pre = max(nums[i], pre + nums[i]) # 返回两个数值中最大的那个
remax = max(remax, pre) # 返回两个数值中最大的那个
return remax # 返回最大值
# 测试用例-python编程时需严格注意缩进,否则程序不能正常运行
# 输入 nums = [-2,1,-3,4,-1,2,1,-5,4]
# 输出 6
if __name__ == '__main__':
nums=[-2,1,-3,4,-1,2,1,-5,4]
re=maxSubArray(nums)
print(re)
【繁华倾夏】【每日力扣题解分享】【Day14】