python算法:最長公共前綴

class Solution:
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if len(strs)==0:
            return ''
        x=0
        y=0
        while x<len(strs) and y<len(strs[x]):
            if x==0:
                word=strs[x][y]
            else:
                if strs[x][y]!=word:
                    break
            if x==len(strs)-1:
                    y+=1
                    x=0
            else:
                x+=1
        return strs[x][:y]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章