Leetcode brushing record-14. El prefijo común más largo

Inserte la descripción de la imagen aquí

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 artículos originales publicados · Me gustaron 14 · Visitantes más de 20,000

Supongo que te gusta

Origin blog.csdn.net/weixin_41545780/article/details/105475317
Recomendado
Clasificación