首页 > 编程语言 > 详细

leetcode Longest Common Prefix python

时间:2015-11-19 20:55:48      阅读:278      评论:0      收藏:0      [点我收藏+]
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

 

leetcode Longest Common Prefix python

原文:http://www.cnblogs.com/allenhaozi/p/4978938.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!