from typing import *
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
strs.sort()
first,end = strs[0],strs[-1]
i=0
res = ""
while i < len(first) and i < len(end):
if first[i] == end[i]:
res += first[i]
i+=1
else:
break
return res
s = Solution()
print(s.longestCommonPrefix(["flower","flow","flight"]))
leetcode 14,最长公共前缀
最新推荐文章于 2024-04-29 09:11:57 发布