当前位置:网站首页>【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,喜歡的可以點個收藏和關注,我們下次再見!
边栏推荐
- VMware install win10 image
- 寒门再出贵子:江西穷县考出了省状元,做对了什么?
- Cloud native cicd framework: Tekton
- MOSFET器件手册关键参数解读
- 【Leetcode】14. 最长公共前缀
- Deep learning image data automatic annotation [easy to understand]
- 流批一体在京东的探索与实践
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
- R and rstudio download and installation tutorial (super detailed)
猜你喜欢
Lampe respiratoire PWM
DigiCert SSL证书支持中文域名申请吗?
[error record] error -32000 received from application: there are no running service protocol
如何与博格华纳BorgWarner通过EDI传输业务数据?
Executive engine module of high performance data warehouse practice based on Impala
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
Download blender on Alibaba cloud image station
基于多元时间序列对高考预测分析案例
寒门再出贵子:江西穷县考出了省状元,做对了什么?
MOSFET器件手册关键参数解读
随机推荐
LeetCode 6. Z 字形变换 (N字形变换)
流批一体在京东的探索与实践
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
大厂面试总结大全
你想要的宏基因组-微生物组知识全在这(2022.7)
Yolov5 practice: teach object detection by hand
Configure MySQL under Linux to authorize a user to access remotely, which is not restricted by IP
如何与博格华纳BorgWarner通过EDI传输业务数据?
Where can I open computer administrator permissions
Ranger (I) preliminary perception
Method of C language self defining function
Global and Chinese markets for airport baggage claim conveyors 2022-2028: technology, participants, trends, market size and share Research Report
Analysis of how to prevent virus in industrial computer
jsp 和 servlet 有什么区别?
二、mock平台的扩展
Talk about an experience of job hopping and being rejected
易语言abcd排序
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
Lampe respiratoire PWM
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share