1. # -*- coding: utf8 -*-
    '''
    __author__ = 'dabay.wang@gmail.com'
    https://oj.leetcode.com/problems/longest-common-prefix/
    14: Longest Common Prefix
  2.  
  3. Write a function to find the longest common prefix string amongst an array of strings.
    ===Comments by Dabay===
    注意边界条件,如果strs为空,直接返回空字符串。
  4.  
  5. 初始化共同前缀为空字符串。
    如果一个字符出现在每个字符的相应位置就把这个字符加到共同前缀中。
    '''
  6.  
  7. class Solution:
    # @return a string
    def longestCommonPrefix(self, strs):
    if len(strs) == 0:
    return ""
    common_pre = ""
    i = 0
    while True:
    to_compare = ""
    for s in strs:
    if i >= len(s):
    return common_pre
    if to_compare == "":
    to_compare = s[i]
    continue
    if s[i] != to_compare:
    return common_pre
    else:
    common_pre = common_pre + to_compare
    i = i + 1
  8.  
  9. def main():
    s = Solution()
    strs = ["abcdef", "abc", "abcd"]
    print s.longestCommonPrefix(strs)
  10.  
  11. if __name__ == "__main__":
    import time
    start = time.clock()
    main()
    print "%s sec" % (time.clock() - start)
  12.  

[LeetCode][Python]14: Longest Common Prefix的更多相关文章

  1. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  2. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  3. C# 写 LeetCode easy #14 Longest Common Prefix

    14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

  4. LeetCode题解(14)--Longest Common Prefix

    https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...

  5. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  6. 【LeetCode】14 - Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...

  7. 【LeetCode】14. Longest Common Prefix (2 solutions)

    Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...

  8. 《LeetBook》leetcode题解(14):Longest Common Prefix[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. Leetcode No.14 Longest Common Prefix最长公共前缀(c++实现)

    1. 题目 1.1 英文题目 Write a function to find the longest common prefix string amongst an array of strings ...

随机推荐

  1. not valid for Running the scheme

    The run destination iPhone6 Plus is not valid for Running the scheme 'MyApp’. Phone6 Plus's iOS 8.0 ...

  2. Javascript 链式运动框架——逐行分析代码,让你轻松了解运动的原理

    所谓链式运动,就是一环扣一环.我们的很多运动实际上来说指的就是分阶段的,第一个阶段动完,下个阶段开始动. 这个链式运动框架就是用来处理这些问题的. 我们先来看下之前的运动框架,以下是Javascrip ...

  3. MFC 双缓冲加载背景

    首先定义DCmemDc和Bitmap CDC DCmemDc: CBitmap memBitmap; CBitmap *oldBitmap; 然后创建一个适应当前内存的DCmemDc CDC * dc ...

  4. Oracle日志性能查看

    http://blog.chinaunix.net/uid-20784775-id-373968.html http://www.orafaq.com/wiki/Scripts https://com ...

  5. yum 安装 5.6

    http://www.cnblogs.com/XBlack/p/5178758.html

  6. zookeeper 手动T掉已挂节点

    zjtest7-redis:/root/zk# cat test_zk.pl use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; use IO ...

  7. jquery validation plugin 使用

    <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Prope ...

  8. yum localinstall rpm

  9. httpclient response 重定向

    HTTPClient请求后,重定向后,获取重定向的URL. 方法一:重定向后获取URL import org.apache.http.HttpEntity; import org.apache.htt ...

  10. 用事件与CSS改变按钮不同状态下的颜色

    目标效果: 表单的群发按钮,在鼠标悬停时为深蓝色,鼠标离开时为淡蓝色. HTML代码: <button id="submitBtn"  class="btn&quo ...