0
点赞
收藏
分享

微信扫一扫

python 实现:字符串的最长公共前缀

四月天2021 2022-02-21 阅读 47
pythongnu

class Solution:
    def longestCommonPrefix(self, strs) -> str:
        data = len(strs[0])
        if len(strs) == 1:
            return strs[0]
        else:
            common = strs[0]
            for x in strs[1:]:
                length = min(len(x), len(common))
                if length <data:
                    data = length
                for i in range(0, length):
                    if len(common) < 1:
                        print("输入不存在公共前缀")
                        return False
                    else:
                        if x[i] != common[i] and data > i:
                            data = i
        return common[0:data]

举报

相关推荐

0 条评论