#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author:wu_tianDe
# datetime:2021/7/17 8:14
# software: PyCharm
class Solution:
def maxSubArray(self, nums:list) -> int:
length = len(nums)
if length==1:
print("只有一个数")
return nums[0]
maxsum,cursum = nums[0],0
for i in range(0,length):
cursum +=nums[i]
if cursum > maxsum:
maxsum = cursum
if cursum < 0:
cursum = 0
return maxsum
if __name__ == '__main__':
s = Solution()
ans = s.maxSubArray([1,2,-7,3])
print(ans)
offer 42. 连续子数组的最大和
最新推荐文章于 2022-03-22 10:27:42 发布