美文网首页
实现最长公共前缀

实现最长公共前缀

作者: 地铁姑娘 | 来源:发表于2018-09-14 16:23 被阅读0次

示例1:

输入['flower','flow','flight']
输出“fl”

示例2:

输入['flower','dog','car']
输出'' ''
class Solution(object):
    def longestCommonPrefix(self,strs):
        '''

        :param strs: List[str]
        :return: str
        '''
        if not strs:
            return ''
        s1 = min(strs)
        s2 = max(strs)
        for i,c in enumerate(s1):
            if c!= s2[i]:
                return s1[:i]
if __name__ == "__main__":
    s = Solution()
    print s.longestCommonPrefix(['flower','dog','car'])
    print s.longestCommonPrefix(['flower','flow','flight'])

结果

image.png

相关文章

  • 实现最长公共前缀

    示例1: 输入['flower','flow','flight'] 输出“fl” 示例2: 输入['flower'...

  • LeetCode 每日一题 [19] 最长公共前缀

    LeetCode 最长公共前缀 [简单] 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回...

  • 14. 最长公共前缀

    20180923-摘抄自14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,...

  • 5,最长公共前缀/数组与字符串

    最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。 示例 1:...

  • Swift 最长公共前缀 - LeetCode

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

  • leetcode探索之旅(14)

    最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。 示例 1: ...

  • Leetcode 14 最长公共前缀

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

  • LeetCodeSwift 14.Longest Common

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

  • [day4] [LeetCode] [title14,122]

    14.最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串""。 示例 ...

  • 14. 最长公共前缀

    14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。 说明...

网友评论

      本文标题:实现最长公共前缀

      本文链接:https://www.haomeiwen.com/subject/kvwogftx.html