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. $store.getters调用不执行

    $store.getters调用不执行 api:https://vuex.vuejs.org/zh/guide/getters.html 场景: 在登录时将登录得到的用户信息存储在vuex的state ...

  2. 韦东山嵌入式Linux学习笔记05--存储管理器

    SDRAM: 原理图如下:          jz2440 v3开发板上面用的内存芯片为钰创科技公司生产的EM63A165TS,一片内存大小为32MB大小,一共有两块,共64MB的大小. SDRAM接 ...

  3. epoll机制和简述

    在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是epoll.相比于select,epoll最大的好处在于它不会随着监听fd数目的 ...

  4. Hibrenate实现根据实体类自动创建表或添加字段

    Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 实现: 在配置hibernate的配置文件中将hbm2ddl.auto设置为update,如:Xml代码&l ...

  5. new一个有父类的对象时各代码块的执行顺序问题

    public class QQ { public static void main(String[] args) { new B(); } } class A { static { System.ou ...

  6. 504错误解决办法 让你的浏览器强制在后端服务器执行而不用通过前端CDN服务器

      因为后端执行时间过长,前端不等待,导致提示504错误的解决办法 504 错误是因为你的CDN服务器设置的延时有限, 超时导致的504 是前端不等待中止,是前端不行,后端应该正常 502 错误是后端 ...

  7. debug --- 使用Eclipse

    debug必知(快捷键若无效,有可能是与其它软件的快捷键发生冲突的原因) 1.F6  ——  单步执行代码,即顺序一行行地执行源码 2.F5  ——  跳入当前调用的函数的内部,即进入函数内部执行源码 ...

  8. Mysql配置查询

    查看mysql数据库的线程数: show global status like 'Thread%'; 如果我们在MySQL服务器配置文件中设置了thread_cache_size,当客户端断开之后,服 ...

  9. k8sService资源

    一.service资源及其实现模型 通过规则定义出由多个pod对象组合而成的逻辑集合,以及访问这组pod的策略.service关联pod资源的规则要借助于标签选择器来完成 1.service资源概述 ...

  10. 爬虫相关基础技术铺垫---多线程Thread和队列Queue应用

    from queue import Queue from threading import Thread class mydownloader(Thread): def __init__(self,q ...