当前位置:网站首页>[leetcode] 14. Préfixe public le plus long
[leetcode] 14. Préfixe public le plus long
2022-07-02 17:01:00 【Wangzirui32】
Blogueur wangzirui32
Oui. - Oui. Collection Attention!~~
Cet article a été publié pour la première fois àCSDN,Reproduction interdite sans autorisation
Table des matières
1. Description du sujet

Source::Boucle de force(LeetCode)
Liens:https://leetcode.cn/problems/longest-common-prefix/
Le droit d'auteur est la propriété du réseau de saisie.Pour les réimpressions commerciales, veuillez contacter l'autorisation officielle,Reproduction non commerciale Veuillez indiquer la source.
2. Comment résoudre le problème
Nous avons trouvé la chaîne la plus courte de la liste,Faire un cycle,Chaque boucle vérifie ensuite si elle est identique aux autres préfixes de chaîne,Si le même résultat est stockéresultMoyenne,Renvoie le préfixe maximum après la fin du cycle.
3. Mise en œuvre du Code
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
Je vais analyser le Code le plus important :
min(strs, key=lambda s: len(s))Avec la longueur de la chaîne comme référence , Sélectionner la chaîne minimaleswith = min_length_str[:i]Obtenir l'indexiPréfixe précédentall([k.startswith(swith) for k in strs])Détecter si tous les autres préfixes de chaîne sont identiques .
4. Résultat final

C'est bon,Voici le cours d'aujourd'hui,Je suiswangzirui32,Comme vous pouvez commander une collection et l'attention,À la prochaine fois.!
边栏推荐
- 数字IC手撕代码--投票表决器
- 电脑自带软件使图片底色变为透明(抠图白底)
- P6774 [NOI2020] 时代的眼泪(分块)
- LeetCode 2. 两数相加
- 易语言abcd排序
- R and rstudio download and installation tutorial (super detailed)
- LeetCode 4. 寻找两个正序数组的中位数(hard)
- What will you do after digital IC Verification?
- LeetCode 1. Sum of two numbers
- Exploration and practice of integration of streaming and wholesale in jd.com
猜你喜欢

Serial port controls steering gear rotation

go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)

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

Ranger (I) preliminary perception

移动应用性能工具探索之路

关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...

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

unity Hub 登錄框變得很窄 無法登錄

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

PWM控制舵机
随机推荐
What if the win11 app store cannot load the page? Win11 store cannot load page
Yolov5 practice: teach object detection by hand
国内比较好的OJ平台[通俗易懂]
Linux Installation PostgreSQL + Patroni cluster problem
Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
How to solve the failure of printer driver installation of computer equipment
Detailed explanation of @accessories annotation of Lombok plug-in
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
AcWing 300. Task arrangement
LeetCode 5. 最长回文子串
Seal Library - installation and introduction
Interview summary of large factories
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
LeetCode 2. Add two numbers
What is the difference between JSP and servlet?
P6774 [NOI2020] 时代的眼泪(分块)
uboot的作用和功能
Digital IC hand tearing code -- voting device
PCL point cloud image transformation