Leetcode刷题记录——14. 最长公共前缀

在这里插入图片描述

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if strs == []:
            return ''
        length = len(strs)
        tempres = ''
        minlength = len(strs[0])
        for thisstr in strs:
            minlength = min(len(thisstr),minlength)
        for i in range(minlength):
            for j,thisstr in enumerate(strs):
                if j == 0:
                    tempres += thisstr[i]
                elif j <= length - 1:
                    if tempres[i] != thisstr[i]:
                        return tempres[:-1]
        return tempres


发布了59 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41545780/article/details/105475317
今日推荐