题解 | #最长公共前缀#

最长公共前缀

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

#
# 
# @param strs string字符串一维数组 
# @return string字符串
#
class Solution:
    def longestCommonPrefix(self , strs ):
        # write code here
        if len(strs) == 0:
            return ""
        l = ""
        m = strs[0]
        for index in range(len(m)):
            num = 1
            for i in strs[1:]:
                if index < len(i):
                    if m[index] == i[index]:
                        num += 1
            if num == len(strs):
                l += m[index]
            if num < len(strs):

                break

        return l
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务