【LeetCode每天一题】Longest Common Prefix(最长前缀)
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(最长前缀)的更多相关文章
- leetcode第14题--Longest Common Prefix
Problems: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. 思路:求最长前缀子 ...
- Leetcode算法刷题:第14题 Longest Common Prefix
Longest Common Prefix 题目 给予一个列表,元素为字符串,写一个程序找出最长公共前缀 解题思路 先比较两个字符串,如果第一个字符不一样,则返回空值,比较完成后,用这个公共字符串和下 ...
- [LeetCode] Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...
- 【LeetCode OJ 14】Longest Common Prefix
题目链接:https://leetcode.com/problems/longest-common-prefix/ 题目:Write a function to find the longest co ...
- [LeetCode] 14. Longest Common Prefix 最长共同前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- # Leetcode 14:Longest Common Prefix 最长公共前缀
公众号:爱写bug Write a function to find the longest common prefix string amongst an array of strings. If ...
- 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 ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【LeetCode算法-14】Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
随机推荐
- simulation vs emulation
Hardware emulation, the use of special purpose hardware to emulate the behavior of a yet-to-be-built ...
- nohup 同时实现记录日志和屏幕输出
nohup nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户/关闭终端之后继续运行相应的进程.nohup就是不挂断 ...
- C#5种方式生成缩略图
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 如何迁移完整SQL数据库到另外一台服务器
如何迁移完整SQL数据库到另外一台服务器: https://jingyan.baidu.com/album/9f7e7ec080d1b36f28155422.html?picindex=1
- dma传输数据长度,在启动前必须确保是一个大于0的数字
否则将导致不能接受数据
- Warning: Failed to halt at after bootloader, forced stop at
该错误证实是因为 cc2650 SW下载模式,芯片复位引脚未接出来导致,芯片复位必须和下载器保持良好连接
- CH 2101 - 可达性统计 - [BFS拓扑排序+bitset状压]
题目链接:传送门 描述 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到y的一条 ...
- MYSQL数据库在Windows系统中重置root密码
1.以系统管理员身份打开cmd 2.查看MySQL是否启动: net start 3.如果启动就停止:net stop MySQL55 注:MySQL55是在我的电脑上的mysql数据库服务名 4.再 ...
- [No0000139]轻量级文本编辑器,Notepad最佳替代品:Notepad++
在详细介绍Notepad++之前,先来解释一下,为何要选择Notepad++,即把常见的一些文本编辑器和Notepad++比较,看看其有哪点好: 常见的文本编辑器有很多,此处,只提及Notepad,N ...
- [No0000132]正确使用密码加盐散列[译]
如果你是一个 web 开发工程师,可能你已经建立了一个用户账户系统.一个用户账户系统最重要的部分是如何保护密码.用户账户数据库经常被黑,如果你的网站曾经被攻击过,你绝对必须做点什么来保护你的用户的密码 ...