mycode   91.59%

class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ''
res = ''
minlen = min([len(s) for s in strs])
for i in range(minlen):
alpha = strs[0][i]
if not all([s[i]==alpha for s in strs[1:]]):
return res
res += alpha
return res

参考

思路 : 从后往前逐步缩短搜索的位置

注意:

s = 'asdc'
s[:8] 是可以输出s的,不会报错

class Solution:
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
# initialize common str as the first in list, loop array and shorten it
if not strs:
return ""
prefix = strs[0]
for s in strs:
n = len(prefix)
while n > 0 and prefix[:n] != s[:n]:
n -= 1
prefix = s[:n]
return prefix

leetcode-easy-string-14 Longest Common Prefix的更多相关文章

  1. leetCode练题——14. Longest Common Prefix

    1.题目 14. Longest Common Prefix   Write a function to find the longest common prefix string amongst a ...

  2. LeetCode记录之14——Longest Common Prefix

    本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...

  3. [LeetCode][Python]14: Longest Common Prefix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  4. Leetcode 14. Longest Common Prefix(水)

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

  5. 14. Longest Common Prefix【leetcode】

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

  6. 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 ...

  7. C# 写 LeetCode easy #14 Longest Common Prefix

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

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

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

  9. 【一天一道LeetCode】#14 Longest Common Prefix

    一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...

  10. LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串

    所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...

随机推荐

  1. colspan和rowspan

    colspan和rowspan这两个属性用于创建特殊的表格. colspan用来指定单元格横向跨越的列数:colspan就是合并列的,colspan=2的话就是合并两列. rowspan用来指定单元格 ...

  2. Hyperledger Fabric(4)链码ChainCode

    智能合约,是一个抽象的概念,智能合约的历史可以追溯到 1990s 年代.它是由尼克萨博(Nick Szabo)提出的理念,几乎与互联网同龄. 我们这里所说的智能合约只狭义的指区块链中.它能够部署和运行 ...

  3. Spingboot使用log4j2

    1.pom.xml加入log4j2,并同时把spring boot默认的logging去掉,注意一定要全部排除否则会报错. <dependency> <groupId>org. ...

  4. Chrome安装Axure插件axure-chrome-extension

    用Chrome打开Axure发布的原型图打不开,提示需要安装axure-chrome-extension插件,如下图提示 下面记录一下安装过程,其实很简单,插件没必要从网上到处找,在你发布的路径下就有 ...

  5. deployment控制pod进行滚动更新以及回滚

    更新pod镜像两种方式: 方式一:kubectl set image deployment/${deployment name} ${container name}=${image} 例: kubec ...

  6. 第三次java测验2

    package java3; public class Suiji { public long a=12345L;//定义long类型的a,b,c变量 public long c=12345L; pu ...

  7. 清北学堂dp图论营游记day4

    依然zhx讲. 讲了概率与期望: 期望:事件结果的平均大小.记作E(x). E(x)=每种结果的大小与其概率的乘积的和. 例如,记掷一枚骰子的点数为x E(x)=1*(1/6)+2*(1/6)+3*( ...

  8. 安装theano时候发现报错:cannot install ''numpy'.It is a distutils installed project and thus we cannot ...

    发现我安装theano的时候需要numpy需要1.9以上版本,而我之前自带的numpy是1.8版本,所以版本有问题.根本原因是theano需要的numpy版本不符合要求,但是numpy已经安装过了,所 ...

  9. CPU性能指标

    1,主频 主频 = 时钟频率,它是指CPU内部晶振的频率,常用单位为MHz,它反映了CPU的基本工作节拍; 时钟频率又称主频,它是指CPU内部晶振的频率,常用单位为MHz,它反映了CPU的基本工作节拍 ...

  10. 详解python编译器和解释器的区别

    高级语言不能直接被机器所理解执行,所以都需要一个翻译的阶段,解释型语言用到的是解释器,编译型语言用到的是编译器. 编译型语言通常的执行过程是:源代码——预处理器——编译器——目标代码——链接器——可执 ...