选取第一个元素一次和后续元素比较,如find方法返回值部位0,则去除尾项,直至返回0:
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if strs==[]:
return ""
a=strs[0]
for i in range(1,len(strs)):
while strs[i].find(a)!=0:
a=a[:-1]
return a