您的位置:首页 > 其它

leetcode 53. Maximum Subarray

2017-02-23 10:59 471 查看
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.    最大子列和

思路:在线搜索O(n)

class Solution(object):

    def maxSubArray(self, nums):

        """

        :type nums: List[int]

        :rtype: int

        """

        s = None;c = 0;j=0

        while j<len(nums):

            c+=nums[j]

            if c>s or s==None:

                s = c

            if c<0:

                c=0

            j+=1

        return s

                    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法