這道題是為了求最長前綴
def longestCommonPrefix(strs):
strs = list(set(strs))
lenth = len(strs)
if strs != []:
pub_str = ""
if strs[0] == "":
return ""
elif lenth == 1 and strs[0] != "" :
return strs[0]
for i in strs[0]:
pub_str += i
print(pub_str)
for j in (1,lenth-1):
if len(pub_str) > len(strs[j]) or not strs[j].startswith(pub_str):
return pub_str[:-1]
else:
continue
else :
return ""
strs = ["aa","a"]
index = longestCommonPrefix(strs)
print(index)
以上代碼為什么多次執行結果不同,時而為None時而為a,是在不解
uj5u.com熱心網友回復:
debug一下查看變數流程就一目了然了。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/128784.html
