[leetcode 14] 最长公共前缀
写一个函数可以查找字符串中的最长公共字符。
示例:
输入:["flower","flow","flight"]
输出:"fl"
输入:["dog","racecar","car"]
输出:" " (不存在公共的字符串)
代码:
class Solution: def longestCommonPrefix(self,strs): """ :type strs: List[str] :rtype: str """ if len(strs) == 0: return "" prefix="" length=1 while length <= min([len(s) for s in strs]): for i in range(1,len(strs)): if strs[i][:length] != strs[i-1][:length]: return prefix result = strs[0][:length] length +=1 return prefix