[leetcode] 14. longest common prefix

Leetcode link for this question

Discription:

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

Analyze:

Code 1:

class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if not strs:
            return ""
        if not strs[0]:
            return ""
        l=0
        tmp=strs[0][l]
        while 1:
            for i in strs:
                if l>=len(i):
                    return strs[0][0:l]
                if i[l] !=tmp:
                    return strs[0][0:l]
            l+=1
            if l>=len(strs[0]):
                    return strs[0][0:l]
            tmp=strs[0][l]
        return strs[0][0:l+1]

Submission Result:

Status: Accepted
Runtime: 52 ms
Ranking: beats 73.43%