給定一個(gè)整數(shù)數(shù)組滞谢,找到一個(gè)具有最大和的子數(shù)組髓迎,返回其最大和硅瞧。
注意事項(xiàng)
子數(shù)組最少包含一個(gè)數(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玩了幾天