当前位置:网站首页>【Leetcode】14. 最長公共前綴
【Leetcode】14. 最長公共前綴
2022-07-02 17:01: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,喜歡的可以點個收藏和關注,我們下次再見!
边栏推荐
- System Verilog implements priority arbiter
- What is generics- Introduction to generics
- Detailed explanation of @accessories annotation of Lombok plug-in
- jsp 和 servlet 有什么区别?
- Take you ten days to easily complete the go micro service series (I)
- 移动应用性能工具探索之路
- 【云原生】简单谈谈海量数据采集组件Flume的理解
- Talk about an experience of job hopping and being rejected
- Serial port controls steering gear rotation
- VMware安装win10镜像
猜你喜欢

【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦

linux下配置Mysql授权某个用户远程访问,不受ip限制

Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation

Understand one article: four types of data index system

Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品

Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation

隐私计算技术创新及产业实践研讨会:学习

LeetCode 2. 两数相加

七张图,学会做有价值的经营分析

Serial port controls steering gear rotation
随机推荐
P6774 [noi2020] tears in the era (block)
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
SQL solves the problem of continuous login deformation holiday filtering
618深度複盤:海爾智家的制勝方法論
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
Serial port controls steering gear rotation
Cloud native cicd framework: Tekton
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
What is normal distribution? What is the 28 law?
流批一体在京东的探索与实践
LeetCode 5. Longest Palindromic Substring
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
什么是泛型?- 泛型入门篇
Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements
Seven charts, learn to do valuable business analysis
linux下配置Mysql授权某个用户远程访问,不受ip限制
学习周刊-总第60期-2022年第25周
Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
MySQL port