14. Longest Common Prefix

問題

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

分析:

最長前綴

Python

class Solution:
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        ans = ""
        i = 0
        while strs:
            if len(strs[0]) > i: 
                currc = strs[0][i]
            else: break
            for s in strs:
                if  len(s) <= i or s[i] != currc:
                    return ans
            ans += currc
            i += 1
        return ans
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章