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, 導致在不但造成 build-in function 失效, 且會導致:
這是因為你對整數
max
, 進行了呼叫總而言之, variable 的命名切記不要與:
keywords
build-in functions
標準庫或任何使用中的 package/module 的名稱
同名。
題外話
以下都是題外話
以你現在這段程式碼來看, 似乎只需要寫一個 function 就好, 寫這個 class 看起來是多餘的 (除非你是在做線上題庫?)
不用特地傳 list 長度進去, Python 中問 list 長度可以很簡單地用
len()
完成對於 LIS 問題, 我簡化了一下你的 code:
只求 LIS 的長度:
整個 LIS 都要求出來:
測驗:
結果:
我回答過的問題: Python-QA