[LeetCode][Python]14: Longest Common Prefix
# -*- 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的更多相关文章
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【一天一道LeetCode】#14 Longest Common Prefix
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- C# 写 LeetCode easy #14 Longest Common Prefix
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- LeetCode题解(14)--Longest Common Prefix
https://leetcode.com/problems/longest-common-prefix/ 原题: Write a function to find the longest common ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
- 【LeetCode】14 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...
- 【LeetCode】14. Longest Common Prefix (2 solutions)
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of ...
- 《LeetBook》leetcode题解(14):Longest Common Prefix[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
随机推荐
- js统计字符串,并且判断出现次数最多的
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- View的工作原理
一.认识ViewRoot和DecorView 当Activity对象被创建的时候,会将DecorView添加到Window中,同时创建ViewRootImpl对象(ViewRoot对应于ViewRoo ...
- android studio SVN的搭建
android studio 安装 SVN:http://www.it165.net/pro/html/201404/11412.html http://jingyan.baidu.com/album ...
- 使用PDO执行SQL语句exec()、query()
在PHP脚本中,通过PDO执行SQL查询与数据库进行交互,可以分为三种不同的策略,使用哪一种方法取决于你要做什么操作. 1.使用PDO::exec()方法 当执行INSERT.UPDATE和DELET ...
- PHP获取真实的网络IP
function get_client_ip() { $ip = $_SERVER['REMOTE_ADDR']; if (isset($_SERVER['HTTP_CLIENT_IP']) & ...
- tornado web框架
tornado web框架 tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web ...
- SQL Server 查看指定表上的索引
解决方案: sys.indexs; ---------------------------------------------------------------------------------- ...
- linux md5 加密字符串和文件方法
linux md5 加密字符串和文件方法 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Message-Digest Algorithm 5),此算法对任意 ...
- JSTL标签急速秒杀jsp页面中的java代码(一)---Core标签库
JSTL标签简介 ===================================================================== JSTL的全称是JavaServer Pa ...
- 全国计算机等级考试二级教程-C语言程序设计_第15章_位运算
位运算,不适用于实数,仅仅适用于整数.字符. C语言的位运算只能操作整数.字符,实数是指数方式表示的,不适用于位运算. #define _CRT_SECURE_NO_WARNINGS #include ...