leetcode:Longest Common Prefix【Python版】

1、当strs为空,直接输出“”

2、当strs中含有“”,直接输出“”

3、strs[0]的最长长度由最短公共长度l决定(code line:15)

 class Solution:
# @return a string
def longestCommonPrefix(self, strs):
if strs == []:
return ""
for i in range(1,len(strs)):
l1 = len(strs[0])
l2 = len(strs[i])
if l1>l2:
l = l2
else:
l = l1
if l==0:
return ""
strs[0]=strs[0][0:l]
for j in range(l):
if strs[0][j] != strs[i][j]:
strs[0] = strs[0][0:j]
break
return strs[0]
上一篇:F. Graph Without Long Directed Paths Codeforces Round #550 (Div. 3)


下一篇:使用JavaMail创建邮件和发送邮件