找到最短的字符串
已list[0]为标准,遍历list,加单个字符串右指针,不同即已到最长公共字串,返回
class Solution(object):
def longestCommonPrefix(self, strs):
if not strs:
return ""
if len(strs) == 1:
return strs[0]
#minl = min([len(x) for x in strs])
l=[] #最短字符串
for i in strs:
l.append(len(i))
minl=min(l)
end = 0
while end < minl:
for i in range(1,len(strs)): #不同便已是最长
if strs[i][end]!= strs[i-1][end]:
return strs[0][:end]
end += 1
return strs[0][:end]
本文介绍了一种算法,用于从一系列字符串中找到最长的共同前缀。通过比较每个字符串的字符,直到发现不同之处,从而确定最长公共子串。
5124

被折叠的 条评论
为什么被折叠?



