题目
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串””。

思路
如果数组长度为0,则返回空字符串””。
以数组中第0个元素作为模板,双指针比较两个字符串的公共元素
common_prefix = ''for ch1, ch2 in zip(str1, str2):if ch1 != ch2:breakelse:common_prefix += ch1
依次遍历字符串数组中的每个字符串,更新最长公共前缀。
class Solution:def longestCommonPrefix(self, strs: List[str]) -> str:if len(strs) == 0: return ''if len(strs) == 1: return strs[0]common_prefix = strs[0]for str_ in strs[1:]:tmp = ''for ch1, ch2 in zip(common_prefix, str_):if ch1 != ch2:breakelse:tmp += ch1common_prefix = tmpreturn common_prefix
