78. 最长公共前缀
78. 最长公共前缀
中文English
给k个字符串,求出他们的最长公共前缀(LCP)
样例
样例 1:
输入: "ABCD", "ABEF", "ACEF"
输出: "A"
样例 2:
输入: "ABCDEFG", "ABCEFG" and "ABCEFA"
输出: "ABC"
class Solution: """ @param strs: A list of strings @return: The longest common prefix """ def longestCommonPrefix(self, strs): # write your code here if len(strs) == 1: return strs[0] elif len(strs) == 0: return '' res = '' #取出strs字符串中的最小值出来 min_l = min([len(s) for s in strs]) i = 0 while i <= min_l: for j in range(len(strs)-1): if strs[j][:i] != strs[j+1][:i]: return strs[j][:i-1] i += 1 return strs[0][:i-1]