本來用的find,發現速度太慢,看了看大神的,是每位做比較,因此改了:
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if len(strs) == 0:
return ""
maxstr = max(strs)
minstr = min(strs)
lens = len(minstr)
result = ""
for i in range(lens):
prefind = minstr[i]
for j in strs:
#key = j.find(prefind)
if prefind != j[i]:
return result
result = minstr[:i+1]
return result