41. 最大子數(shù)組

給定一個整數(shù)數(shù)組,找到一個具有最大和的子數(shù)組,返回其最大和。

注意事項

子數(shù)組最少包含一個數(shù)
給出數(shù)組[?2,2,?3,4,?1,2,1,?5,3],符合要求的子數(shù)組為[4,?1,2,1],其最大和為6
2017.11.17

class Solution:
    """
    @param: nums: A list of integers
    @return: A integer indicate the sum of max subarray
    """
    def maxSubArray(self, nums):
        # write your code here
        temp = None
        max = None
        for i in nums:
            if temp == None:
                temp = i
            else:
                temp += i
            if max == None:
                max = temp
            if max <= temp:
                max = temp
            if temp <= 0:
                temp = 0
                #思路是,依次相加,如果加到出現(xiàn)負(fù)數(shù)或零,就放棄前面所有數(shù)
                #然后記錄最大值
        return max

2017.11.20玩了幾天

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容