class LongestIncreasingSubsequence:
def getLIS(self, A, n):
# write code here
dp=[0 for i in range(n)]
dp[0]=1
max=0
print dp
for i in range(n):
now=0
if i!=0:
res=1
for j in range(i):
if A[i]>A[j]:
res=dp[j]
now=now +1
if now>=max:
max=now
dp[i]=res+1
else:
dp[i]=res
print dp
#return max(dp)
kk=LongestIncreasingSubsequence()
kk.getLIS([1,4,2,5,3],5)
其中dp 是一个以int类型为成员的list
而使用max()函数时却会报错
TypeError: 'int' object is not callable
是由什么原因导致的?
你的max函数在第五行被赋值成0了,max函数被覆盖了,你的变量改个名不要跟库函数重名了
原因如同 @rayleisure 所说, 你在这里使用
max
作为参考到int
型态的 variable, 导致在max
作為參考到int
型態的 variable, 導致在時不但造成 build-in function 失效, 且會導致:
這是因為你對整數
max
, 進行了呼叫總而言之, variable 的命名切記不要與:
keywords
build-in functions
標準庫或任何使用中的 package/module 的名稱
同名。
題外話
以下都是題外話
以你現在這段代碼來看, 似乎只需要寫一個 function 就好, 寫這個 class 看起來是多餘的 (除非你是在做線上題庫?)
不用特地傳 list 長度進去, Python 中問 list 長度可以很簡單地用
时不但造成 build-in function 失效, 且会导致:len()
max
, 进行了呼叫keywords
build-in functions
标准库或任何使用中的 package/module 的名称
题外话
🎜以下都是题外话🎜🎜🎜以你现在这段代码来看, 似乎只需要写一个 function 就好, 写这个 class 看起来是多余的 (除非你是在做线上题库?)🎜🎜 🎜🎜不用特地传 list 长度进去, Python 中问 list 长度可以很简单地用
len()
完成🎜🎜 🎜🎜对于 LIS 问题, 我简化了一下你的 code:🎜🎜 🎜 🎜只求 LIS 的长度:🎜 🎜整个 LIS 都要求出来:🎜 🎜测试:🎜 rrreee 🎜结果:🎜 rrreee 🎜 🎜🎜我回答过的问题🎜: Python-QA🎜