牛客
Leetcode
【思路1】动态规划
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
for i in range(1, len(nums)):
nums[i] += max(nums[i - 1], 0)
return max(nums)
'''
作者:jyd
链接:https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/solution/mian-shi-ti-42-lian-xu-zi-shu-zu-de-zui-da-he-do-2/
来源:力扣(LeetCode)
'''
【思路2】itertools.accumulate函数
- accumulate 函数的功能是对传进来的iterable对象逐个进行某个操作(默认是累加)
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
return max(itertools.accumulate(nums, lambda x, y: max(x+y, y)))
'''
作者:ji-zhi-hao-sama
链接:https://leetcode-cn.com/problems/lian-xu-zi-shu-zu-de-zui-da-he-lcof/solution/python-yi-xing-by-ji-zhi-hao-sama/
来源:力扣(LeetCode)
'''