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 ...
随机推荐
- abap 优化之ST05
DATA: gt_mara TYPE TABLE OF mara. SELECT * INTO TABLE gt_mara FROM mara WHERE MATKL = 'L000001' %_hi ...
- MySQL中导出用户权限设置的脚本
在对MySQL数据库进行迁移的时候,有时候也需要迁移源数据库内的用户与权限.对于这个迁移我们可以从mysql.user表来获取用户的相关权限来生成相应的SQL语句,然后在目标服务器上来执行生成的SQL ...
- 解决xshell小键盘输入时串码(干货!!)
点击文件——属性 点击终端,修改为Linux即可
- Gym - 101915D Largest Group 最大团
给你一个二分图 问你最大团为多大 解一:状压DP 解二:二分图最大匹配 二分图的最大团=补图的最大独立集 二分图最大独立集=二分图定点个数-最大匹配 //Hungary #include<bit ...
- 设计数据结构之LRU缓存
leetcode 146. LRU Cache class LRUCache { private: struct Node { int key; int val; Node* prev; Node* ...
- css 点击打开遮罩
<div> <nav class="bar bar-tab"> <a class="tab-item external" href ...
- 前端杂谈: Attribute VS Property
前端杂谈: Attribute VS Property 第一个问题: 什么是 attribute & 什么是 property ? attribute 是我们在 html 代码中经常看到的键值 ...
- mysql 8.0.18 手工安装记录
mysql 8.0.18 手工安装记录 为了日常方便,特记录如下. 一.安装系统依赖包 #.系统依赖包安装 yum -y install make gcc-c++ cmake bison-devel ...
- Python CGI编程Ⅴ
通过CGI程序传递 Textarea 数据 Textarea 向服务器传递多行数据,HTML代码如下: textarea.py 脚本代https://www.xuanhe.net/码如下: 修改 te ...
- 科普TPF知识
https://tieba.baidu.com/p/4926092734?see_lz=1&pn=1 707680700 https://tieba.baidu.com/p/492609273 ...