Longest Common Prefix - LeetCode

来源:互联网 发布:淘宝泳衣女装连体 编辑:程序博客网 时间:2024/05/29 14:05

Longest Common Prefix - LeetCode

题目:

Write a function to find the longest common prefix string amongst an array of strings.


分析:

题意:找到字符串列表中最长公共前缀。 如题意,应该还是挺简单的。

代码:

代码中,为了找到两个字符串中的公共前缀,新建了一个函数。
class Solution:    # @return a string    def longestCommonPrefix(self, strs):        if not strs:            return ""        temp = strs[0]        for i in strs[1:]:                temp = self.sameintwostr(temp,i)        return temp            def sameintwostr(self,str1,str2):        temp = ""        for i in range(min(len(str1),len(str2))):            if str1[i] == str2[i]:                temp += str1[i]            else:                return temp        return temp


0 0