最长公共前缀(14)
法一:
class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if not strs: return '' str_min = min(strs) str_max = max(strs) for i in range(len(str_min)): if str_min[i] != str_max[i]: return str_min[:i] return str_min 法二:
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
ret = ''
for i in zip(*strs):
if len(set(i)) ==1:
ret+=i[0]
else:
break
return ret