Python 属性错误:';列表';对象没有属性';复印件';在leetcode编辑器中,但在我的PyCharm中,它';没有错
我正在使用python解决leetcode中的“最长公共前缀”问题。这是我的密码:Python 属性错误:';列表';对象没有属性';复印件';在leetcode编辑器中,但在我的PyCharm中,它';没有错,python,Python,我正在使用python解决leetcode中的“最长公共前缀”问题。这是我的密码: class Solution: # @param {string[]} strs # @return {string} def longestCommonPrefix(self, strs): if strs is None or strs == []: return "" if "" in strs: return "" minList=[
class Solution:
# @param {string[]} strs
# @return {string}
def longestCommonPrefix(self, strs):
if strs is None or strs == []:
return ""
if "" in strs:
return ""
minList=[]
tempList=[]
prefix=""
if len(strs)>0:
minLen = len(strs[0])
for str in strs:
if len(str)<minLen:
minLen = len(str)
for str in strs:
if len(str)==minLen:
minList.append(str)
if len(minList)==1:
prefix = minList[0]
else:
while True:
isAllEqual = True
for min in minList:
if min!=minList[0]:
isAllEqual=False
if isAllEqual:
prefix=minList[0]
break
else:
for min in minList:
tempList.append(min[:-1])
minList.clear()
minList=tempList.copy()
tempList.clear()
if prefix == "":
return prefix
for string in strs:
if prefix in string:
continue
else:
while prefix:
prefix = prefix[:-1]
if prefix =="":
return ""
if prefix in string:
break
return prefix
我是新手,谢谢你的帮助!谢谢 Python3
list
类有一个clear()
方法,但Python2list
类没有。这很可能是问题的根源。leetcode似乎将使用Python 2运行您的脚本,因此您也应该使用Python 2进行开发,以避免类似的不兼容。您正在使用Python 2解释器来运行此代码,即Python 3代码。有几种解决方案。
你可以用一个
或者在终端中调用python3解释器。像
$ python3 myscript.py
可能重复的
#!/usr/bin/python3
$ python3 myscript.py