python实现列表中查找最大和的子序列


def test_func2(num_list):
'''
求数组中最大子序列的和,子序列必须连续
'''
length=len(num_list)
max_value=-10000000000
tmp=0
p = 0
q = 0
t= []
for i in range(length):
if tmp+num_list[i] p = i
q=1
else:
q+=1
tmp = max(tmp + num_list[i], num_list[i])
max_value=max(max_value, tmp)
if tmp == max_value:
t.append((p, (p + q)))

print(max_value,t[-1])


if __name__ == '__main__':
a= [5,-7,3,5,-2,4,-1]
# a =[5,2,-8,3]
test_func2(a)


转载于:https://www.cnblogs.com/aisir/p/9593776.html

你可能感兴趣的:(python实现列表中查找最大和的子序列)