【leetcode 简单】第五题 最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
示例 1:
输入: ["flower","flow","flight"]
输出: "fl"
示例 2:
输入: ["dog","racecar","car"]
输出: ""
解释: 输入不存在公共前缀。
说明:
所有输入只包含小写字母 a-z 。
class Solution:
@classmethod
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ''
new_strs=[i for i in strs if len(i) != 0] if new_strs:
strs_length = len(new_strs)
if strs_length != len(strs):
return ''
else:
return '' if strs_length == 1:
return strs[0]
example=strs[0]
strs.remove(example) tmp=1 while len([i for i in strs if example[:tmp] == i[:tmp]]) == strs_length-1 and tmp <= len(example):
tmp += 1
tmp-=1
return example[:tmp] if example[:tmp] else ''
class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
if not strs:
return ""
shortest=min(strs,key=len)
for x, y in enumerate(shortest):
for s in strs:
if s[x]!=y:
return shortest[:x]
return shortest
【leetcode 简单】第五题 最长公共前缀的更多相关文章
- LeetCode刷题-最长公共前缀(简单)
题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow ...
- 【Leetcode】【简单】【14最长公共前缀】【JavaScript】
题目 14. 最长公共前缀 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",& ...
- 【LeetCode】Longest Common Prefix(最长公共前缀)
这道题是LeetCode里的第14道题. 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["f ...
- LeetCode(14):最长公共前缀
Easy! 题目描述: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower",&qu ...
- [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 最长公共前缀
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- 14. Longest Common Prefix[E]最长公共前缀
题目 Write a function to find the longest common prefix string amongst an array of strings. If there i ...
- 【python】Leetcode每日一题-最长公共子序列
[python]Leetcode每日一题-最长公共子序列 [题目描述] 给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度.如果不存在 公共子序列 ,返回 0 . ...
- Leetcode题库——14.最长公共前缀
@author: ZZQ @software: PyCharm @file: longestCommonPrefix.py @time: 2018/9/16 17:50 要求:查找字符串数组中的最长公 ...
随机推荐
- 第七周PSP(10.27-11.03)
psp 进度条 项目 细则 总计 代码行数 0 随笔字数 0 知识点 无 累计曲线 饼图
- hash值
任何类都继承public int hashCode()方法,该方法返回的值是通过将该对象的内部地址转换为一个整数来实现的,hash表的主要作用就是在对对象进行散列的时候作为key输入.我们需要每个对象 ...
- react-router之代码分离
概念 无需用户下载整个应用之后才能访问访问它.即边访问边下载.因此我们设计一个组件<Bundle>当用户导航到它是来动态加载组件. import loadSomething from 'b ...
- Mac & how to uninstall LANDesk
Mac & how to uninstall LANDesk http://eddiejackson.net/wp/?p=9036 https://community.ivanti.com/d ...
- 【移动端debug-2】Flexbox在移动端的兼容实践
最近在项目中用到了flexbox,总结一下使用心得. 一.什么是flexbox,干嘛使的? 曾几何时,我们特别希望能像word一样,在排版时有个分散对齐选项(平均分配子元素宽度)这样我就可以任意在父元 ...
- 第123天:移动web开发中的常见问题
一.函数库 underscoreJS _.template: <ol class="carousel-indicators"> <!--渲染的HTML字符串--& ...
- idea Class<>表示的含义
- 洛谷P1144 最短路计数 及其引申思考
图论题目练得比较少,发一道spfa的板子题目- 题目:P1144 题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: ...
- 创建Django工程-Day19
1. 新建一个day19的工程和app01. 2. 新建templates和static的文件夹. 3. 去settings.py中去做配置. 1)注释掉csrf 2)配置模板路径 'DIRS': [ ...
- 【BZOJ3667】Rabin-Miller算法(Pollard_rho)
[BZOJ3667]Rabin-Miller算法(Pollard_rho) 题面 呜,权限题,别问我是怎么做的(我肯定没有权限号啊) 第一行:CAS,代表数据组数(不大于350),以下CAS行,每行一 ...