最大子序列的查找的python 实现
问题:
问题1 的代码实现:
#coding:utf-8 #py2.7 def findmaxsubstr(nums): bufs={} for i,num in enumerate(nums): if i==0: bufs[i]=num else: bufs[i]=bufs[i-1]+num if bufs[i-1]>=0 else num print bufs b=max([(bufs[i],i) for i in bufs.keys()]) print b rs=[] for i in range(b[1],-1,-1): if bufs[i]>=0: rs=[nums[i]]+rs else: break if len(rs) == 0: return [max(nums)] else: return rs print findmaxsubstr([-1,-2,-3,4,5,7,-12,89,-12,33,-99,1,-23,0,55])
问题2的代码实现: