class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
ls=len(strs)
if ls==0:
return ""
elif ls==1:
return strs[0]
else:
minL=2**32-1
for s in strs:
if minL>len(s):
minL=len(s)
pf=s
i=0
while i<ls:
s1=strs[i]
a=len(pf)
b=len(s1)
p=0
q=0
while p<a:
if pf[p] != s1[q]:
if p==0:
return ""
else:
pf=pf[:p]
break
p+=1
q+=1
i+=1
return pf
python leetcode 14. Longest Common Prefix
最新推荐文章于 2024-08-15 01:55:01 发布