先找出数组中最长和最短的字符串,最长公共前缀即为这两个字符串的公共前缀
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs: return ""
str0 = min(strs)
str1 = max(strs)
for i in range(len(str0)):
if str0[i] != str1[i]:
return str0[:i]
return str0
作者:you-yuan-de-cang-qiong
链接:https://leetcode-cn.com/problems/longest-common-prefix/solution/zi-dian-xu-zui-da-he-zui-xiao-zi-fu-chuan-de-gong-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。