1 最int 型数组,子数组的最大和

#get max array
#得到 最大 子数组
def getmaxarray(arraylist):
    
    currentsum = arraylist[1]
    currentsumlist = [arraylist[1]]
    maxsum = arraylist[1]
    maxsumlist = [arraylist[1]]

    for i in range(len(arraylist)):
        # 如果加入后面的数据大于当前的sum
        if( (currentsum +  arraylist[i]) >  currentsum):
            # 更新当前的currentsum 数据
            currentsum += arraylist[i]
            # 更新当前的currentsumlist 列表
            currentsumlist.append(arraylist[i])

            # 如果当前的sum 大于最大sum  更新最大sum
            if(currentsum >= maxsum):
                maxsum = currentsum
                maxsumlist = currentsumlist
        else:
            currentsum = arraylist[i]
            currentsumlist= []
            currentsumlist.append(arraylist[i])
            # 如果当前的sum 大于最大sum  更新最大sum
            if(currentsum >= maxsum):
                maxsum = currentsum
                maxsumlist = currentsumlist
    return maxsumlist


print(getmaxarray([-1,-3,-10,-5]))
print(getmaxarray([-1,-3,-0,-10,-5]))
print(getmaxarray([1,-3,0,10,-5]))
print(getmaxarray([1,3,0,10,5]))

Continue reading