Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "".

Example 1:            Input: ["flower","flow","flight"]                    Output: "fl"

Example 2:            Input: ["dog","racecar","car"]                     Output: ""                                  Explanation: There is no common prefix among the input strings.

Note:  All given inputs are in lowercase letters a-z.

思路


  在看到这道题的时候我选择的是直接进行查找。从第一个字符串中提取出一个字符然后对每一个字符串相应位置进行比较。如果不先等直接返回结果。相等则继续向一下一位查找。时间复杂度为O(s*n)(s为最短字符串的长度, n为列表的长度), 空间复杂度为O(s)。

  第二种办法是使用字典树来解决,但是需要我们先对列表中的字符串进行构造成字典树,最后从字典树的根节点进行查找当有分支的时候就停止。时间复杂度为O(x)(x是列表中所有字符串长度之和), 时间复杂度为O(y)(字典树的大小)。

图示


第一种解法图示

第二种解法的图示

代码


 class Solution(object):
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if len(strs) < : # 长度小于2直接返回
return strs[] if strs else '' res_str, min_len = '', len(min(strs)) # 设计结果返回量和最短的字符串长度
i =
while i < min_len: # 从小标第一个开始
tem = strs[][i] # 取出第一个比较
for j in strs: # 从第列表中第一个开始遍历
if j[i] != tem: # 如果不相等直接返回
return res_str
res_str += tem
i +=
return res_str

【LeetCode每天一题】Longest Common Prefix(最长前缀)的更多相关文章

  1. leetcode第14题--Longest Common Prefix

    Problems:Write a function to find the longest common prefix string amongst an array of strings. 就是返回 ...

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

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

  3. Leetcode算法刷题:第14题 Longest Common Prefix

    Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...

  4. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

  5. 【LeetCode OJ 14】Longest Common Prefix

    题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...

  6. [LeetCode] 14. Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  7. # Leetcode 14:Longest Common Prefix 最长公共前缀

    公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...

  8. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

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

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

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

  10. 【LeetCode算法-14】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

随机推荐

  1. MSDN2001 快捷方式丢失的处理方法

    1.  C:\Windows\hh.exe,右键 - 发送快捷方式到桌面 2. 给快捷方式改名为:MSDN2001 3. 右键快捷方式 - 属性- 快捷方式选项卡中的目标填写: C:\Windows\ ...

  2. svn的安装方法

    SVN的安装很常用.但是我好像经常用了之后就忘记.这次把笔记放在这里,如果以后忘了,就可以随时查看了. 步骤: 一.下载SVN 这里常用的是site.zip. 之前是手头有现成的site.zip.今天 ...

  3. {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析

    MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...

  4. 将 django部署到 heroku上

    为什么要这样做,因为我并没有弄懂,使用传统和推荐的方法来部署django.刚好我手里有这么一份教程我就按照这么做了. 1. 访问 http://heroku.com/ 完成一个注册,注册需要一个国外的 ...

  5. Codeforces 677D - Vanya and Treasure - [DP+优先队列BFS]

    题目链接:http://codeforces.com/problemset/problem/677/D 题意: 有 $n \times m$ 的网格,每个网格上有一个棋子,棋子种类为 $t[i][j] ...

  6. [No0000EA]C# 可空类型(Nullable)

    C# 可空类型(Nullable) C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基础值类型正常范围内的值,再加上一个 null 值. 例如,Nullable& ...

  7. 如何使用ffmpeg

    https://blog.csdn.net/minger1202/article/details/52468986  解码 https://www.jianshu.com/p/c6cfe2edd083 ...

  8. dbclient python ---influxdb -install -relay--http write--read.[create db]

    1s=1000ms 1ms=1000 microseconds 1microsecond=1000 nanoseconds+01:00 from influxdb import InfluxDBCli ...

  9. [skill][http] http管道化连接

    已知http的请求响应是一对一的. 就是一个请求跟着接下来的响应便是与之配对了. 而另一种方式, 可以依靠顺序, 即发送多个http请求, 然后返回对个http响应. 严格按照顺序将他们对应起来, 称 ...

  10. 图->连通性->有向图的强连通分量

    文字描述 有向图强连通分量的定义:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly co ...