当前位置:网站首页>【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,喜歡的可以點個收藏和關注,我們下次再見!
边栏推荐
- 小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
- uboot的作用和功能
- LeetCode 1. 两数之和
- 机器学习-感知机模型
- VMware install win10 image
- What if the win11 app store cannot load the page? Win11 store cannot load page
- Kubernetes three open interfaces first sight
- P6774 [NOI2020] 时代的眼泪(分块)
- 学习周刊-总第60期-2022年第25周
- Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
LeetCode 2. Add two numbers
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
Lampe respiratoire PWM
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
pwm呼吸燈
Résumé de l'entrevue de Dachang Daquan
PWM breathing lamp
电脑自带软件使图片底色变为透明(抠图白底)
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array
Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
随机推荐
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
What if the win11 app store cannot load the page? Win11 store cannot load page
国内比较好的OJ平台[通俗易懂]
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
上传代码到远程仓库报错error: remote origin already exists.
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Digital IC hand tearing code -- voting device
大廠面試總結大全
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
PWM breathing lamp
unity Hub 登錄框變得很窄 無法登錄
你想要的宏基因组-微生物组知识全在这(2022.7)
机器学习-感知机模型
TCP server communication process (important)
C语言中sprintf()函数的用法
流批一体在京东的探索与实践
PWM控制舵机
Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era