Leetcode-Python 最长公共前缀

Leetcode-Python 最长公共前缀_第1张图片

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        
        string = ''
        if len(strs) != 0:
            string = min(strs,key = lambda s:len(s))
        end = len(string)
        for s in strs:
            i = end
            while i >= 0:
                if string[:i] == s[:i]:
                    end = i
                    break
                else:
                    i -= 1
            if i == -1:
                return ''

        return string[:end]

 github项目地址:https://github.com/JockWang/LeetCode-Python

你可能感兴趣的:(Leetcode)