当前位置:网站首页>【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,喜歡的可以點個收藏和關注,我們下次再見!
边栏推荐
- TCP congestion control details | 2 background
- MySQL port
- Role and function of uboot
- Learning Weekly - total issue 60 - 25th week of 2022
- linux下配置Mysql授权某个用户远程访问,不受ip限制
- Ap和F107数据来源及处理
- Day 18 of leetcode dynamic planning introduction
- Interview summary of large factories
- John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
- 深度学习图像数据自动标注[通俗易懂]
猜你喜欢

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

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

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

如何与博格华纳BorgWarner通过EDI传输业务数据?

Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))

Penetration tool - intranet permission maintenance -cobalt strike

How openharmony starts FA of remote devices

Multi task prompt learning: how to train a large language model?

OpenPose的使用

A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
随机推荐
R and rstudio download and installation tutorial (super detailed)
什么是泛型?- 泛型入门篇
618深度複盤:海爾智家的制勝方法論
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
DigiCert SSL证书支持中文域名申请吗?
易语言abcd排序
2322. 从树中删除边的最小分数(异或和&模拟)
C语言自定义函数的方法
基于Impala的高性能数仓实践之执行引擎模块
VMware install win10 image
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
Kubernetes three open interfaces first sight
MySQL port
Take you ten days to easily complete the go micro service series (I)
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
隐私计算技术创新及产业实践研讨会:学习
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share