# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
https://oj.leetcode.com/problems/longest-common-prefix/
14: Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings.
===Comments by Dabay===
注意边界条件,如果strs为空,直接返回空字符串。 初始化共同前缀为空字符串。
如果一个字符出现在每个字符的相应位置就把这个字符加到共同前缀中。
''' 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 def main():
s = Solution()
strs = ["abcdef", "abc", "abcd"]
print s.longestCommonPrefix(strs) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[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. IScroll5+在ios、android点击(click)事件不兼容解决方法

    Bug描述: ios.android4.4+下不能触发click事件. Bug解决: 调用iscroll插件,增加配置参数:click:true/false click的值是要根据移动终端设备进行判断 ...

  2. ContentProvider类的解析

    一.ContentProvider类 1.作用:专门用于不同应用之间进行数据共享的方式. 二.实现方法 1.创建ContenteProvider类 步骤一:继承ContentProvider接口,重写 ...

  3. Oracle EBS-SQL (INV-2):检查帐户别名发放记录.sql

    SELECT FU.description                 操作者,         ITM.SEGMENT1               物料编码,         ITM.DESC ...

  4. Hibernate 多表关联映射- 一对多关系映射(one-to-many)

    Hibernage.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...

  5. 关于nodejs,request模块的一个bug

    今天在使用request时发生了一个错误, 对方网站的证书设置的不正确导致本地请求不能返回数据: 解决方案是在配置request时加入一个忽略证书验证得字段: 具体代码如下 request.post( ...

  6. 楼天城楼教主的acm心路历程(作为励志用)

    楼主个人博客:小杰博客 利用假期空暇之时,将这几年GCJ,ACM,TopCoder 參加的一些重要比赛作个 回顾.昨天是GCJ2006 的回顾,今天时间上更早一些吧,我如今还清晰记得3 年 前,我刚刚 ...

  7. jquery第四期:对象转换的小实例

    为了更清晰的看懂jquery对象是一个数组,我们这一期来改变每一个<li>中的值,在前面加上序号. 代码如下: <!DOCTYPE html PUBLIC "-//W3C/ ...

  8. hdu 5256 序列变换 (LIS变形)

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. 使用trim方法检测用户输入

    首先需要封装trim方法,可以去除字符串两端空格的方法 function trim(str) { return str.replace(/^\s+|\s+$/g, ""); } 获 ...

  10. c++中参数传递和函数返回简析

    1.参数传递: 每次调用函数时,都会重新创建该函数所有的形参,此时所传递的实参将会初始化对应的形参.形参的初始化与变量的初始化一样. 非引用类型:如果形参具有非引用类型,则复制实参的值.普通的非引用类 ...