14. 最长公共前缀

作者: 不爱去冒险的少年y | 来源:发表于2018-05-15 19:38 被阅读1次

14. 最长公共前缀

编写一个函数来查找字符串数组中的最长公共前缀。

如果不存在公共前缀,返回空字符串""。

示例 1:

输入: ["flower","flow","flight"]输出:"fl"

示例 2:

输入: ["dog","racecar","car"]输出:""解释:输入不存在公共前缀。

说明:

所有输入只包含小写字母a-z。

class Solution:

    def longestCommonPrefix(self, strs):

        """

        :type strs: List[str]

        :rtype: str

        """

        new_strs = []

        if len(strs) == 0:

            return ""

        good = True

        for i in range(len(strs[0])):

            for j in range(len(strs)):

                if len(strs[j])==i:

                    good = False

                    break

                str = strs[0][i]

                if str == strs[j][i]:

                    continue

                else:

                    good = False

                    break

            if good:

                new_strs.append(strs[0][i])

            else:

                break

        return ''.join(new_strs)

相关文章

  • 14. 最长公共前缀

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

  • 算法第3天:最长公共前缀

    leetcode 14. 最长公共前缀 simple 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共...

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

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

  • 14. 最长公共前缀

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

  • 14.最长公共前缀

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

  • 14. 最长公共前缀

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

  • [LeetCode]14-最长公共前缀

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

  • 每日Leetcode—算法(2)

    14.最长公共前缀 输入: ["flower","flow","flight"],输出: "fl"输入: ["do...

  • 14. 最长公共前缀

    14. 最长公共前缀[https://leetcode-cn.com/problems/longest-commo...

  • LeetCode学习计划:LeetCode 75-Level-2

    14. 最长公共前缀[https://leetcode.cn/problems/longest-common-pr...

网友评论

    本文标题:14. 最长公共前缀

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