leetcode-easy-string-14 Longest Common Prefix
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的更多相关文章
- leetCode练题——14. Longest Common Prefix
1.题目 14. Longest Common Prefix Write a function to find the longest common prefix string amongst a ...
- LeetCode记录之14——Longest Common Prefix
本题虽然是easy难度,题目也一目了然,问题就是在这里,需要考虑的特殊情况太多,太多限制.导致我一点点排坑,浪费了较多时间. Write a function to find the longest ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- Leetcode 14. Longest Common Prefix(水)
14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...
- 14. Longest Common Prefix【leetcode】
14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- 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 ...
- C# 写 LeetCode easy #14 Longest Common Prefix
14.Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...
- [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
一天一道LeetCode系列 (一)题目: Write a function to find the longest common prefix string amongst an array of ...
- LeetCode 14. Longest Common Prefix字典树 trie树 学习之 公共前缀字符串
所有字符串的公共前缀最长字符串 特点:(1)公共所有字符串前缀 (好像跟没说一样...) (2)在字典树中特点:任意从根节点触发遇见第一个分支为止的字符集合即为目标串 参考问题:https://lee ...
随机推荐
- vue项目中使用mockjs+axios模拟后台数据返回
自己写练手项目的时候常常会遇到一个问题,没有后台接口,获取数据总是很麻烦,于是在网上找了下,发现一个挺好用的模拟后台接口数据的工具:mockjs.现在把自己在项目中使用的方法贴出来 先看下项目的目 ...
- 利用druid sql parser搞一些事情
在最近的项目开发中,有这样一个需求,就是给定一个查询的sql,在where语句中添加几个条件语句.刚开始想的是,是否能用正则去做这个事情呢?其实不用语法树还是有一点困难的. 经过一系列google,看 ...
- js 继承的简单理解
什么是继承 js中的继承就是获取存在对象已有属性和方法的一种方式. 继承一 属性拷贝 就是将对象的成员复制一份给需要继承的对象. // 创建父对象 var superObj = { name:'liy ...
- CDH5.16.1升级kafka0.10到1.0.1
激活1.0.1的包
- 微信小程序(7)--微信小程序连续旋转动画
微信小程序连续旋转动画 https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-animation.html <view animation=&quo ...
- VirtualBox给CentOS虚拟机挂载磁盘扩大空间
VirtualBox给CentOS虚拟机挂载磁盘扩大空间 楼主,发现虚拟机使用存储空间不够用的情况,需要改虚拟机挂载磁盘,扩容,在网上找了一波资料,于是整合记录操详细作如下: 概要步骤如下: 1.设置 ...
- Ubuntu中出现“Could not get lock /var/lib/dpkg/lock”的解决方法
在运行Ubuntu安装软件,使用命令sudo apt-get install时,有时会出现以下的错误: E: Could not get lock /var/lib/dpkg/lock - open ...
- PAT Basic 1002 写出这个数 (20 分)
读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1. 输出格式: 在一行内输出 n 的 ...
- Circle HDU - 6550 (数学)
在半径为 1 的圆上有 n 个点,它们也是圆的 n 等分点,将每个相邻的 n 等分点相连,组成了一个正 n边形,现在你可以在圆上再增加一个点,使得新的 n + 1 边形的面积最大,请输出最大面积. I ...
- java8学习之内部迭代与外部迭代本质剖析及流本源分析
关于Stream在Java8中是占非常主要的地位的,所以这次对它进行进一步探讨[这次基本上都是偏理论的东东,但是理解它很重要~],其实流跟咱们数据库学习当中的sql语句的特点是非常非常之像的,为什么这 ...