题解 | #最长公共前缀#

最长公共前缀

http://www.nowcoder.com/practice/28eb3175488f4434a4a6207f6f484f47


#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param strs string字符串一维数组 
# @return string字符串
#
class Solution:
    def longestCommonPrefix(self , strs: List[str]) -> str:
        # write code here
        max_len=""
        word_list=[]
        max=5000 ##设置初始最短的字符串长度
        for i in strs:
            word_len= len(i)
            if word_len<max:
                max=word_len
                max_len=i  ##找到最小的字符串
        for i in strs: #判断最小的字符串在不在每个字符串里面
            if max_len in i:
                continue
            else:
                max_len=max_len[:-1] #不在的话就结尾截取1个
        return max_len
            
        
            
            
                


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务