标签:更新 比较 col color class get 赋值 连续子数组 def
sample input:
-1,4,-3,6,-20,4,-2,5
sample output:
7
最容易想到的就是暴力解决方法,穷举所有连续子数组的可能性,进行比较,复杂度O(n2)
代码略
代码如下(python3):
def getMaxSub(arr): max = arr[0] sum = arr[0]
i = 1 while(i<len(arr)): sum += arr[i] if(sum<=0): sum = 0 elif(sum>max): max = sum i += 1 print(‘max sum of submatrix is ‘,max) #for test getMaxSub([-1,4,-3,6,-20,4,-2,5])
输出结果为“max sum of submatrix is 7”
标签:更新 比较 col color class get 赋值 连续子数组 def
原文地址:https://www.cnblogs.com/zxpnotebook/p/10096286.html