解题思路:min和max
在Python里字符串是可以比较的,按照ascII值排,举例abb, aba,abac,最大为abb,最小为aba。所以只需要比较最大最小的公共前缀就是整个数组的公共前缀
min(["flower","flow","flight"]) #flight
max(["flower","flow","flight"]) #'flower'
for i,x in enumerate(min(["flower","flow","flight"])):
print (i,x)
'''
0 f
1 l
2 i
3 g
4 h
5 t
'''
def longestCommonPrefix(self, strs) :
if not strs:
return ""
s1 = min(strs)
s2 = max(strs)
for i,x in enumerate(s1):
if x != s2[i]:
return s2[:i]
return s1