【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 ...
随机推荐
- 英语语言能力挑战游戏: anagrams & palindromes
基于英语语言的知名游戏(可以归类为智商挑战题): anagrams anagram定义为一个有着相同的字母的不同的词,例: stop的anagram为:tops, opts, pots, and sp ...
- manjaro 设置 国内源
注意,如果安装过程中出现无法连接服务,请参看第 4条. 1. 设置官方镜像源(包括 core, extra, community, multilib ) $ sudo pacman-mirrors - ...
- Spring实战 难懂的JavaBean
bean中文解释为:豆; 豆形种子; 毫无价值的东西. 按照上面的意思,很难理解Bean是个什么鬼,Java豆? 我们先来看一个典型的JavaBean,直观地理解下: public class Per ...
- 网络通信协议七之ARP工作过程及工作原理解析
ARP(地址解析协议) 局域网: ARP地址解析协议用于将计算机的网络IP地址转化为物理MAC地址,ARP协议的基本功能就是通过目标设备的IP地址.查询目标设备的MAC地址,以保证通信的顺利进行.在每 ...
- 洛谷 P1059明明的随机数 & P1068分数线划定 & P1781宇宙总统
题目:https://www.luogu.org/problemnew/show/P1059 思路:STL中的set使用. //#include<bits/stdc++.h> #inclu ...
- Linux之文档与目录结构 目录的相关操作 Linux的文件系统
Linux之文档与目录结构 Linux文件系统结构 Linux目录结构的组织形式和Windows有很大的不同.首先Linux没有“盘(C盘.D盘.E盘)”的概念.已经建立文件系统的硬盘分区被挂载到 ...
- [No0000104]JavaScript-基础课程4
要说 JavaScript 和其他较为常用的语言最大的不同是什么,那无疑就是 JavaScript 是函数式的语言,函数式语言的特点如下: 函数为第一等的元素,即人们常说的一等公民.就是说,在函数式编 ...
- centos7安装zabbix3.4
一.系统环境 关闭防火墙及selinux systemctl stop firewalld.service systemctl disable firewalld.service sed -i 's/ ...
- winform excel导入--NPOI方式
项目中要用到excel导入数据,用NPOI方式做了一个demo,记录如下: Form1代码: public Form1() { InitializeComponent(); } private voi ...
- Page7:能控性、能观性及其判据和对偶原理(2)[Linear System Theory]
内容包含连续时间时变系统的能控性和能观测性判据,离散时间线性系统的能控性和能观测性判据,以及对偶原理