当前位置:网站首页>【Leetcode】14. 最长公共前缀
【Leetcode】14. 最长公共前缀
2022-07-02 14:18:00 【wangzirui32】
博文作者 wangzirui32
喜欢的可以 点赞 收藏 关注哦~~
本文首发于CSDN,未经许可禁止转载
1. 题目描述

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-common-prefix/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2. 解题思路
我们找到列表中最小长度的字符串,进行循环,然后每一次循环检测是否与其他字符串前缀相同,如相同结果存入result中,循环结束后返回最大前缀。
3. 代码实现
Code:
class Solution:
def longestCommonPrefix(self, strs) -> str:
min_length_str = min(strs, key=lambda s: len(s))
result = ""
for i in range(1, len(min_length_str)+1):
swith = min_length_str[:i]
if all([k.startswith(swith) for k in strs]):
result = swith
return result
我来解析其中比较重要的代码:
min(strs, key=lambda s: len(s))以字符串的长度为参照,选出最小字符串swith = min_length_str[:i]获取索引i之前的前缀all([k.startswith(swith) for k in strs])检测其他字符串前缀是否都相同。
4. 最终结果

好了,今天的课程就到这里,我是wangzirui32,喜欢的可以点个收藏和关注,我们下次再见!
边栏推荐
- Ap和F107数据来源及处理
- jsp 和 servlet 有什么区别?
- DigiCert SSL证书支持中文域名申请吗?
- linux安装postgresql + patroni 集群问题
- Ranger (I) preliminary perception
- Role and function of uboot
- La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
- pwm呼吸灯
- LeetCode 3. 无重复字符的最长子串
- Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

大廠面試總結大全

Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes

机器学习-感知机模型

易语言abcd排序

What is normal distribution? What is the 28 law?

Digital IC hand tearing code -- voting device

流批一体在京东的探索与实践

In MySQL and Oracle, the boundary and range of between and precautions when querying the date

Serial port controls steering gear rotation

How to choose the right kubernetes storage plug-in? (09)
随机推荐
LeetCode 6. Z 字形变换 (N字形变换)
LeetCode 6. Zigzag transformation (n-shaped transformation)
System Verilog实现优先级仲裁器
Deep learning image data automatic annotation [easy to understand]
大廠面試總結大全
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
sql解决连续登录问题变形-节假日过滤
Classic quotations
< IV & gt; H264 decode output YUV file
Cloud native cicd framework: Tekton
[cloud native] briefly talk about the understanding of flume, a massive data collection component
一文看懂:数据指标体系的4大类型
Analysis of how to prevent virus in industrial computer
你想要的宏基因组-微生物组知识全在这(2022.7)
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
Vscode setting delete line shortcut [easy to understand]
What will you do after digital IC Verification?
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~