美文网首页
Leetcode-14题:Longest Common Pref

Leetcode-14题:Longest Common Pref

作者: 八刀一闪 | 来源:发表于2016-09-24 16:51 被阅读8次

    题目:

    Write a function to find the longest common prefix string amongst an array of strings.

    代码:

    class Solution(object):
        def longestCommonPrefix(self, strs):
            """
            :type strs: List[str]
            :rtype: str
            """
            if strs==None or len(strs)==0:
                return ''
            res = strs[0]
            for str in strs:
                i = 0
                while i<min(len(res),len(str)) and res[i]==str[i]:
                    i += 1
                if i == 0:
                    return ''
                else:
                    res = res[:i]
            return res
            
    

    相关文章

      网友评论

          本文标题:Leetcode-14题:Longest Common Pref

          本文链接:https://www.haomeiwen.com/subject/vzmxyttx.html