Leetcode-14题:Longest Common Pref

2016-09-24  本文已影响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
        
上一篇 下一篇

猜你喜欢

热点阅读