题解 | #最长公共前缀#
最长公共前缀
https://www.nowcoder.com/practice/28eb3175488f4434a4a6207f6f484f47
class Solution: def longestCommonPrefix(self , strs: List[str]) -> str: # write code here if not strs: return '' if len(strs) == 1: return strs[0] res = [i for i in strs[0]] idx = 0 for i in range(1, len(strs)): word = strs[i] while idx < len(res): if idx > len(word)-1: res = res[:idx] break elif res[idx] != word[idx]: res = res[:idx] break elif res[idx] == word[idx]: idx += 1 idx = 0 return ''.join(res)