
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
# 以第一个字符串为基准
prefix = strs[0]
for s in strs[1:]:
# 缩减 prefix 直到它是当前字符串的前缀
while not s.startswith(prefix):
prefix = prefix[:-1]
if not prefix: # 如果前缀为空,返回 ""
return ""
return prefix
731

被折叠的 条评论
为什么被折叠?



