Leetcode学习(22)—— Maximum Subarray

来源:互联网 发布:海康网络摄像机密码 编辑:程序博客网 时间:2024/06/07 16:28

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6.
class Solution(object):    def maxSubArray(self, nums):        current_sum = max_sum = nums[0]        for num in nums[1:]:            current_sum = max(num, current_sum + num)            max_sum = max(max_sum, current_sum)        return max_sum

这里写图片描述

0 0
原创粉丝点击