当前位置:网站首页>[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.!
边栏推荐
- <四> H264解码输出yuv文件
- Cell:清华程功组揭示皮肤菌群的一种气味挥发物促进黄病毒感染宿主吸引蚊虫...
- 隐私计算技术创新及产业实践研讨会:学习
- Where can I open computer administrator permissions
- IP地址转换地址段
- ROW_ NUMBER()、RANK()、DENSE_ Rank difference
- What is generics- Introduction to generics
- 入行数字IC验证后会做些什么?
- Cloud native cicd framework: Tekton
- IP address translation address segment
猜你喜欢

Take you ten days to easily complete the go micro service series (I)

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

OpenHarmony如何启动远程设备的FA

Easy language ABCD sort

七张图,学会做有价值的经营分析
![John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])

你想要的宏基因组-微生物组知识全在这(2022.7)

July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance

Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!

Exploration and practice of integration of streaming and wholesale in jd.com
随机推荐
数字IC手撕代码--投票表决器
电脑自带软件使图片底色变为透明(抠图白底)
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
【Leetcode】13. 罗马数字转整数
Penetration tool - intranet permission maintenance -cobalt strike
TCP server communication process (important)
What is generics- Introduction to generics
Masa framework - DDD design (1)
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
DGraph: 大规模动态图数据集
MySQL port
有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
TCP拥塞控制详解 | 2. 背景
远程办公对我们的各方面影响心得 | 社区征文
Student course selection system (curriculum design of Shandong Agricultural University)
Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
二、mock平台的扩展