题解 | #最长公共前缀#

最长公共前缀

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

思路如下: 找出第一个串和第二个串的公共前缀s,然后找s和第三个串的公共前缀s2,依次进行到最后一个即可

# 
# @param strs string字符串一维数组 
# @return string字符串
#
class Solution:
    def longestCommonPrefix(self , strs ):
        # write code here
        if len(strs)==0 or strs=="":
            return ""
        pre = strs[0]
        i=1
        while i<len(strs):
            while pre != strs[i][:len(pre)]:
                pre = pre[:(len(pre)-1)]
            i+=1
        return pre

全部评论

相关推荐

彧未sr:查看图片
投递牧原集团等公司7个岗位
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务