NC55 最长公共前缀(python)

题目描述

编写一个函数来查找字符串数组中的最长公共前缀。
示例1

输入

["abca","abc","abca","abc","abcc"]

返回值

"abc"
#
# 
# @param strs string字符串一维数组 
# @return string字符串
#
class Solution:
    def longestCommonPrefix(self , strs ):
        # write code here
        if len(strs)==0:
            return ""
        elif len(strs)==1:
            return strs[0]
        else:
            re = ""
            for i in range(len(strs[0])):
                for j in strs[1:]:
                    if len(j)==i or strs[0][i]!=j[i]:
                        return re
                re = re+strs[0][i]
            return re

 

posted @ 2021-01-10 15:53  蒋励  阅读(122)  评论(0编辑  收藏  举报