您的位置:首页 > 编程语言 > Python开发

leetcode Longest Common Prefix python

2015-11-19 20:17 603 查看
class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) <= 0:
return ""
shortestLen=None
for tmp in strs:
if len(tmp) < shortestLen or shortestLen == None:
shortestLen = len(tmp)
if shortestLen <= 0:
return ""
if len(strs) == 1:
return strs[0]
bolStop=False
res=""
firstStr=strs[0]
del strs[0]

for index in range(shortestLen):
strCarry=""
for tmpStr in strs:
strNext = tmpStr[index]
strCur = firstStr[index]
if strNext != strCur:
bolStop=True
break
elif strNext == strCur:
strCarry=strCur
if bolStop:
break
res+=strCarry

return res
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: