from typing import List class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if not strs: return "" 取第一个字符串作为初始公共前缀 prefix = strs[0] for s in strs[1:]: while s.find(prefix)!= 0: 如果当前字符串不以prefix开头,截掉prefix最后一个字符继续尝试 prefix = prefix[:-1] if not prefix: return "" return prefix