码迷,mamicode.com
首页 > 其他好文 > 详细

Longest Common Prefix

时间:2014-10-09 15:05:24      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   sp   div   c   on   

 

class Solution:
    # @return a string
    def longestCommonPrefix(self, strs):
        num_items=len(strs)
        if num_items<1:
            return ""
        prefix=strs[0]
        for i in strs[1:num_items]:
            if len(prefix)>len(i):
                prefix=prefix[0:len(i)]
            min_len=min(len(i),len(prefix))
            for j in range(min_len):
                if prefix[j]!=i[j]:
                    prefix=prefix[0:j]
                    break
        return prefix

if __name__ == __main__:
    s=Solution()
    #strs=["abd","abcd","abcf","abchrg"]
    strs=["flower","flow","flight"]
    print(s.longestCommonPrefix(strs))

 

Longest Common Prefix

标签:style   blog   color   io   for   sp   div   c   on   

原文地址:http://www.cnblogs.com/iois/p/4012575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!