# -*- 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. javascript 数据结构和算法读书笔记 > 第五章 队列

    队列是一种列表,但是它只能够在队尾插入元素,在队首删除元素.队列用于存储按照顺序排列的数据,先进先出.而栈则是后入栈的元素反而被优先处理. 实际中一般被应用在进程池.排队操作上面. 1. 队列的操作 ...

  2. 移动网络山寨版(OpenBTS)的意义或者无意义 【1】

    在美国内华达州北部,靠近加州的峡谷中,有一片平坦的沙漠,名叫黑岩沙漠(Black Rock Desert).自从1986年以来,每年夏天,在这片沙漠中,都会举办一个为期八天的狂欢节.这个狂欢节的名字叫 ...

  3. css案例学习之div与span的区别

    代码: <html> <head> <title>div 标记范例</title> <style type="text/css" ...

  4. 传智播客C/C++学员荣膺微软&Cocos 2d-x黑客松最佳创新奖

     6月30日,历时32小时的微软开放技术Cocos 2d-x 编程黑客松在北京望京微软大厦成功落下帷幕,这是微软开放技术首次联合Cocos 2d-x 在中国举办黑客松.此次活动共有包括传智播客C/ ...

  5. Hadoop HA部署

    因为公司旧系统的Hadoop版本是2.2,所以在部署新系统时使用了旧系统. 但是在部署ResourceManager auto failover时发现其他nodemanager总是向0.0.0.0请求 ...

  6. 数值分析1:三角函数的计算(C语言实现)

    之前学习C语言的时候,一直有个疑问,计算机从芯片设计的角度来看,只能计算常规的加减乘及移位之类的操作,那么对于像sin .cos这些三角函数,人脑尚无可以直接运算的法则,那么计算机是怎么实现的呢?最近 ...

  7. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  8. web中使用扫描枪

    扫描枪实际上就是一输入设备,只不过它每次在输入的内容后面添加一个回车.因此在web中可以使用js监听回车事件.处理代码如下: jQuery(function() {        jQuery(doc ...

  9. 【Lucene3.6.2入门系列】第10节_Tika

    首先贴出来的是演示了借助Tika创建索引的HelloTikaIndex.java PS:关于Tika的介绍及用法,详见下方的HelloTika.java package com.jadyer.luce ...

  10. 图论+dp poj 1112 Team Them Up!

    题目链接: http://poj.org/problem?id=1112 题目大意: 有编号为1~n的n个人,给出每个人认识的人的编号,注意A认识B,B不一定认识A,让你将所有的人分成两组,要求每组的 ...