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. Docker搭建Gitlab服务器

    1.使用docker搜索gitlab镜像 docker search gitlab 2.下载镜像: docker pull docker.io/gitlab/gitlab-ce 3.查看docker镜 ...

  2. 简单易用的字符串模糊匹配库Fuzzywuzzy

    简单易用的字符串模糊匹配库Fuzzywuzzy 阅读目录 FuzzyWuzzy 简介 安装 用法 已知移植 FuzzyWuzzy 简介 FuzzyWuzzy 是一个简单易用的模糊字符串匹配工具包.它依 ...

  3. jumpserver开源堡垒机部署安装

    0x01.前言 Jumpserver 是全球首款完全开源的堡垒机,使用 GNU GPL v2.0 开源协议,是符合 4A 的专业运维审计系统. Jumpserver 使用 Python / Djang ...

  4. 工具安装——linux下安装JDK1.8

    1.查看Linux环境自带JDK 使用命令:# rpm -qa|grep gcj 显示内容其中包含相应信息# java-x.x.x-gcj-compat-x.x.x.x-xxjpp# java-x.x ...

  5. 搭建CentOs7的WebServer

    CentOs7,在安装的时候,自己可以定义一些东西,包括硬盘分区,服务器角色等. 这一些就搭了一个BasicWebServer,这样的话,里面的很多勾选,包括Java,Perl,Python,php等 ...

  6. D-query SPOJ - DQUERY (莫队算法裸题)

    Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...

  7. java8学习之groupingBy源码分析

    继续接着上一次[http://www.cnblogs.com/webor2006/p/8366083.html]来分析Collectors中的各种收集器的实现, 对里它里面有个groupingby() ...

  8. cypress

    https://community.cypress.com/docs/DOC-14090 usb3.0 only支持

  9. selenium相关导入By、Keys、WebDriverWait、ActionChains,显示等待与隐式等待

    # -*- coding: utf-8 -*- """ @author: Dell Created on Tue Dec 24 12:33:56 2019 "& ...

  10. grunt-contrib-cssmin CSS压缩以及合并

    grunt-contrib-cssmin:压缩以及合并CSS文件 安装插件:npm install grunt-contrib-cssmin --save-dev 不设置compatibility与n ...